Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Why search code not working....

C#
private void cmdsearch_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedItem="ID")
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id='" + textBox3.Text + "'", conn);
        DataTable tbl = new DataTable();
        adp.Fill(tbl);
        dataGridView1.DataSource = tbl;
    }
    else if (comboBox1.SelectedItem="Name")
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where name='" + textBox3.Text + "'", conn);
        DataTable tbl = new DataTable();
        adp.Fill(tbl);
        dataGridView1.DataSource = tbl;
    }
}
Posted
Updated 24-Mar-14 1:10am
v2

1 solution

1.You should debug you application, inspect both SQL used in the SqlDataAdapter and copy the SQL commands and test them in you SQL server then you will see the error.
2.From your code I suspect that in you first SQL, where ID is used, the ID is an integer. If this is the case you should correct this SQL like in below:
C#
SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id=" + textBox3.Text, conn);

Better is to have a validation in your code and to try to convert the value to int before to send the user input to the SQL.
3.In the case of 2nd SQL also you should have a validation and to try to prevent the SQL injection attacks. Here is a MSDN link about.[^]
 
Share this answer
 
Comments
manish7664 24-Mar-14 4:52am    
Thanks
Raul Iloc 24-Mar-14 7:31am    
If my solution helped you, you could accept it, otherwise let me know!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900