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
Soft CornerPosted Apr 6, 2011, 2:45 AM
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 ..
Shyja AbrahamPosted Apr 6, 2011, 1:56 AM
Suthish NairPosted Apr 5, 2011, 12:43 PM
Suthish NairPosted Apr 2, 2011, 2:29 AM
Parameterized Query and SQL Injection Attacks
You need to reset or clear the Gridview every time.
Suthish NairPosted Apr 2, 2011, 2:24 AM
Ketul RathodPosted Apr 2, 2011, 2:18 AM
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;
}