Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Search Data from database by combobox into datagridview
my table:- uid navarchar(50)[Datatype]
Name Varchar(50)[Datatype]// dispalyname in combobox

ERROR:- Invalid column name 'mb08'(uid).

C#
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
       {

           string qry = "select * from tbmobile where uid=" + comboBox2.SelectedValue;
           SqlDataAdapter da = new SqlDataAdapter( qry,con);
           DataTable dt = new DataTable();
           da.Fill(dt);
           dataGridView1.DataSource = dt;
           dataGridView1.Refresh();
       }
Posted
Updated 19-Aug-14 22:57pm
v3
Comments
Ashi0891 20-Aug-14 4:45am    
check the query again. make sure you have correct table name and column name in your sql query. other than that try giving some value to uid that exists in the table and try to execute it in sqlserver itself.
Richard MacCutchan 20-Aug-14 6:58am    
Like I said before, use proper parameterized queries. Do not use string concatenation to build your commands.

You should use SelectedItem as described here[^]. You should also be using proper parameterised queries, especially as you have no idea what will be returned from the combobox.
 
Share this answer
 
Try following changes in your code.
C#
string qry = "select * from tbmobile where uid='" + comboBox2.SelectedValue + "'";

Hope it will work.
 
Share this answer
 
Comments
Bernhard Hiller 20-Aug-14 7:08am    
Works, though I'd recommend a parameterized query. And that would have avoided this problem already...
Sharmanuj 20-Aug-14 8:20am    
You should use the following query.
string qry = "select * from tbmobile where uid='" + comboBox2.SelectedValue.ToString() + "'";

ComboBox.SelectedValue should be converted into String.

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