GridView and Combo

  • Thread starter Thread starter Hari
  • Start date Start date
H

Hari

Hi,

I am using a GridView and a combo box for displaying a list of
companies and upon selection the list in GridView will change. For this


protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
SqlDataSource2.SelectCommand = "SELECT * FROM [contract]
WHERE [cust_id] = " + cmbCompany.Text;
}
}

protected void cmbCompany_SelectedIndexChanged(object sender,
EventArgs e)
{
SqlDataSource2.SelectCommand = "SELECT * FROM [contract] WHERE
[cust_id] = " + cmbCompany.Text;
}

Only the second method works. Why? Any Idea?.
 
You are barking up the wrong tree, if you want the data in a GridView to be
dependent on the selection in a DropdownList then make then add a control
parameter to your SQLDataSource control that points at the DropDownList,
everything will then just work without having to write any code at all.
 
Also you should use paramters in you SQL statements rather than

"SELECT field1, field2 from table 1 where field 2 = " + SomeVariable

because this leaves you open to SQL injection attacks.
 
Back
Top