GridView and Combo

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?.
 
G

Guest

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top