Send Parameter to Grid

BILAL

New Member
I am attempting to send a selected value of a html drop down server control to a asp data grid control. The weirdness I am encountering is that the user selection is not being posted - the value associated with the first option is being sent...Please excuse the text wrapping:<BR><BR>Private Sub Page_Load(etc...)<BR> Dim myConnection As New SqlConnection("server=pcswin2k1;uid=sa;pwd=;database=Northwind")<BR> Dim myCommand As SqlCommand = New SqlCommand("SELECT CustomerID, CompanyName...etc", myConnection)<BR> myConnection.Open()<BR> Dim myReader As SqlDataReader = myCommand.ExecuteReader<BR> Select1.DataValueField = "CustomerID"<BR> Select1.DataTextField = "CompanyName"<BR> Select1.DataSource = myReader<BR> Select1.DataBind()<BR> 'Select1.Items.Insert(0, "Select a value and Search")<BR> myConnection.Close()<BR> End Sub<BR><BR>Private Sub Button1_Click(etc...)<BR> Dim myConnection1 As New SqlConnection("server=pcswin2k1;uid=sa;pwd=;database=Northwind")<BR> Dim myCommand1 As SqlCommand = New SqlCommand("SELECT ...etc WHERE CustomerID = @CompanySelection", myConnection1)<BR> myCommand1.Parameters.Add(New SqlParameter("@CompanySelection", SqlDbType.NVarChar, 5))<BR> 'Following line is a problem - you would think that there<BR> 'would be a way to grab the value associated with a <BR> 'selectedIndex<BR> myCommand1.Parameters("@CompanySelection").Value = http://aspmessageboard.com/archive/index.php/Select1.Value<BR> myConnection1.Open()<BR> Dim myReader1 As SqlDataReader = myCommand1.ExecuteReader<BR> DataGrid1.DataSource = myReader1<BR> DataGrid1.DataBind()<BR> myConnection1.Close()<BR> End SubA couple of things.<BR><BR>In your Page_Load() Handler you only want to Bind to the Data if !IsPostback.<BR><BR>Also you may need to replace this..<BR><BR> myCommand1.Parameters("@CompanySelection").Value = Select1.Value<BR><BR>With this..<BR><BR> myCommand1.Parameters("@CompanySelection").Value = Select1.SelectedItem.Value<BR><BR>Finally (although you probably know this already), where you do this..<BR><BR> myConnection1.Close()<BR><BR>You could also have done this..<BR><BR> Dim myReader1 As SqlDataReader = myCommand1.ExecuteReader(CommandBehavior.CloseConn ection)<BR><BR><BR>Thanks for the response - needed to conditionally handle postback! FYI - Select1.SelectedItem.Value is applicable to the asp list box server control - Selected1.Value is applicable towards the html list box server control..
 
Back
Top