I want to implement a search method in which the user can select the search type from a combobox and enter search value in textbox.
The search button code is here but when I click on the search button, result datagridview is empty.
What is the problem?
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True";
con.Open();
cmd.Connection = con;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandText = "select * from person where @parameter1=@parameter";
if (comboBox1.SelectedIndex == 0)
{
cmd.Parameters.AddWithValue("@parameter1", "name");
}
else
{
cmd.Parameters.AddWithValue("@parameter1", "code");
}
cmd.Parameters.AddWithValue("@parameter",textBox1.Text);
da.SelectCommand = cmd;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}