Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i want to search data using combobox(bind with databse)but show this following error

ERROR:-The parameterized query '(@id varchar(50))select * from tbmobile where uid=@id' expects the parameter '@id', which was not supplied.

my code:-
C#
private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
       {
           SqlCommand cmd = new SqlCommand();
           cmd.CommandText = "select * from tbmobile where uid=@id";
           cmd.Connection = con;
           cmd.Parameters.Add("@id",SqlDbType.VarChar, 50).Value = comboBox1.SelectedValue;
           cmd.ExecuteNonQuery();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           dataGridView1.DataSource = dt;
           dataGridView1.Refresh();
       }
Posted
Updated 25-Aug-14 20:29pm
v2
Comments
Neetin_1809 26-Aug-14 1:38am    
Can U Check For Display Menber & Value Member of Combobox?
Nishant.Chauhan80 26-Aug-14 1:45am    
i also check correct display member & value member

helping link

SqlCommand.Parameters Property
C#
private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from tbmobile where uid=@id";
cmd.Connection = con;
cmd.ParametersParameters.AddWithValue("@id",comboBox1.SelectedValue);//add parameter with value may be it is creating any exception.
//remove or comment following line
//cmd.ExecuteNonQuery();

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
} 


Hope so it'll work.
 
Share this answer
 
Comments
Nishant.Chauhan80 26-Aug-14 1:58am    
thankyou so much sir
MuhammadUSman1 26-Aug-14 4:19am    
You Welcome :)
you are passing varchar parameter while required parameter is int
cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(comboBox1.SelectedValue);
 
Share this answer
 
You should change your code like this (make no sense to execute non query an given SELECT query) to manage also the case where nothing is selected in your combo box:
C#
private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if(comboBox1.SelectedValue == null)
{
 //No parameter for this case!
 cmd.CommandText = "select * from tbmobile";
}
else
{
 cmd.CommandText = "select * from tbmobile where uid=@id";
 cmd.Parameters.Add("@id",SqlDbType.VarChar, 50);
 cmd.Parameters["@id"]=comboBox1.SelectedValue;
}
//
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
}
 
Share this answer
 
Comments
Nishant.Chauhan80 26-Aug-14 1:59am    
thnks its working
Raul Iloc 26-Aug-14 2:04am    
Welcome, I am glad that I could help you!

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