hai,
i have a form with a several textboxes and datagridview. when i enter the name of the student the names are displayed in the datagridview. but i have a problem that ,the values in datagridview are doubled each time enter the textboxes. plz help.
i use this code .
private void textBox14_TextChanged(object sender, EventArgs e)
{
namegrid.Visible = true;
cmd.CommandText = "select staffname from staff where staffname like '" + textBox14.Text + "%'";
da.SelectCommand = cmd;
da.Fill(ds, "item");
DataTable dt = ds.Tables["item"];
namegrid.DataSource = dt;
}
Loading

Ketul Rathod6 reputationPosted Apr 1, 2011, 10:18 PM EDT
U can Write in Any button click Event........
Or U Can True the autopostback properties of TextBoxe.........
private void Button_Clicked(object sender, EventArgs e)
{
namegrid.Visible = true;
cmd.CommandText = "select staffname from staff where staffname like '" + textBox14.Text + "%'";
da.SelectCommand = cmd;
da.Fill(ds, "item");
DataTable dt = ds.Tables["item"];
namegrid.DataSource = dt;
}
Suthish NairRank #92 · 22.8K reputationPosted Apr 1, 2011, 10:24 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 1, 2011, 10:29 PM EDT
Parameterized Query and SQL Injection Attacks
You need to reset or clear the Gridview every time.
Suthish NairRank #92 · 22.8K reputationPosted Apr 5, 2011, 8:43 AM EDT
Shyja Abraham66 reputationPosted Apr 5, 2011, 9:56 PM EDT
Soft CornerRank #1,221 · 1.5K reputationPosted Apr 5, 2011, 10:45 PM EDT
as suthish told "You need to reset or clear the Gridview every time." is rgt , apply this in your code
i have 1 sample code for you :
private void textBox1_TextChanged(object sender, EventArgs e)
{
namegrid.DataSource = null;
if (textBox1.Text != "")
{
namegrid.Visible = true;
//Replace Your Connection String
SqlConnection cnn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=MyDb;Integrated Security=true");
//Replace your query here
SqlDataAdapter da = new SqlDataAdapter("select studentname from schoolinfo where studentid like '" + textBox1.Text + "%'", cnn);
DataSet ds = new DataSet();
//place table name
da.Fill(ds, "schoolinfo");
namegrid.DataSource = ds.Tables["schoolinfo"];
}
}
Hope this will help you ..