Anyways, my problem is this: I have a couple DropDownLists that are being populated from a database. Once the page loads, I am trying to set it up where the user can select an item from the DropDown, and then using a value associated the item in the DropDown, search the database again, and pull all matching records and place them in a DataGrid.
My code looks something like this:
<script runat="server"
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
StatesDropDownList()
End IF
End SubSub StatesDropDownList()
Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection (strConnection)
Dim strSqlStates As String = "SELECT DISTINCT State FROM Properties"Dim objCommand As New OleDbCommand(strSqlStates, objConnection)
Dim objReader As OleDbDataReader = NothingobjConnection.Open()
objReader = objCommand.ExecuteReader()ddlStatesFilter.DataSource = objReader
ddlStatesFilter.DataTextField = "State"
ddlStatesFilter.DataValueField = "State"
ddlStatesFilter.DataBind()
objConnection.Close()
Dim DefaultState As ListItem = New ListItem
DefaultState.Text = "Select a State"
DefaultState.Value = "0"
ddlStatesFilter.Items.Insert(0, DefaultState)End Sub
Sub DoSearch(Source as Object, E as EventArgs)
Dim strStates As String = ddlStatesFilter.SelectedItem.Value
Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=" & Server.Mappath("\data\globalresorts.mdb")
Dim objConnection As New OleDbConnection(strConnection)
Dim strSQL As String = "SELECT * FROM Properties WHERE State =" & ddlStatesFilter.SelectedItem.Value & " ORDER BY ID"Dim objAdapter as New OleDbDataAdapter(strSQL, objConnection)
Dim objDataSet As New DataSet()
objAdapter.Fill(objDataSet, "dtProperties")dgProperties.PagerStyle.NextPageText = "Next"
dgProperties.PagerStyle.PrevPageText = "Previous"dgProperties.DataSource = objDataSet.Tables.Item("dtProperties")
dgProperties.DataBind()End Sub
</script>
And then the control and the DataGrid look a little something like this:
<form runat="server>
<asp:literal ID="lbStatesFilter" runat="server" Text="State: " />
<asp:dropdownlist ID="ddlStatesFilter" runat="server" Font-Names="Verdana" Font-Size="8pt" />
<asp:button ID="SubmitSearch" runat="server" OnClick="DoSearch" Text="Search Listings" Font-Names="Verdana" Font-Size="8pt" Width="150" />
<asp:Datagrid id="dgProperties" runat="server"
AutoGenerateColumns="false"
Font-Size="8pt"
CellPadding="8"
AlternatingItemStyle-BackColor="#CCCCCC"
Font-Names="Verdana"
Width="100%"
AllowPaging="True"
OnPageIndexChanged="GridPageChange"
PageSize="20">
<HeaderStyle font-bold="True" forecolor="White" backcolor="#005BAC"></HeaderStyle>
<EditItemStyle backcolor="WhiteSmoke"></EditItemStyle>
<AlternatingItemStyle backcolor="Gainsboro"></AlternatingItemStyle>
<ItemStyle backcolor="WhiteSmoke"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="ID" HeaderText="ID" DataFormatString="{0:N0}" />
<asp:hyperlinkcolumn HeaderText="Resort Name" datanavigateurlfield="ID" datanavigateurlformatstring="details.aspx?id={0}" datatextfield="Resort_Name" />
<asp:BoundColumn DataField="City" HeaderText="City" />
<asp:BoundColumn DataField="State" HeaderText="State" />
<asp:BoundColumn DataField="Bedrooms" HeaderText="Bedrooms" />
<asp:BoundColumn DataField="Week" HeaderText="Week" />
<asp:BoundColumn DataField="Sale_Price" HeaderText="Sale Price" DataFormatString="{0:c2}" />
<asp:BoundColumn DataField="Rent_Price" HeaderText="Rent Price" DataFormatString="{0:c2}" />
</Columns>
</asp:Datagrid>
</form>
And here is the error that I am getting when I try and run it:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.
Source Error:
Line 149:
Line 150: Dim objDataSet As New DataSet()
Line 151: objAdapter.Fill(objDataSet, "dtProperties")
Line 152:
Line 153:dgProperties.PagerStyle.NextPageText = "Next"Source File: D:\...\search.aspx Line: 151
Thanks in advance for any help on this one. And sorry for so much code, but Im just not sure where the problem is coming from, so I want to make sure I give all the facts.
-JasonWithout looking at your code, basic concpets.
On page load, populate the drop down lists, enclose this in the If Page.IsPostBack = False, so it only happens once, obviously this can change based on your requirements.
Next, have a method that populates the datagrid, such as the dropdownlist selectedindexchange. Just use the selectedindex, value to hit the database to populate the datagrid.
0 comments:
Post a Comment