Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Dear Friends,

C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=192.168.1.4;Initial Catalog=ParamBills;Persist Security Info=True;User ID=sa;Password=login";
            conn.Open();
            SqlCommand cmd = new SqlCommand("select Product_Name,Product_Code,Item_Name,Item_code,qty from Master_Stock_Register where Product_Name='" + textBox1.Text + "'", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill (dt);
            dataGridView1.DataSource = dt;
          // dataGridView1.DataBind();
            conn.Close();
        }


This solution is: when i changed text box value the data grid values also changed.. but i need add one by one below grid.. please help me..
Posted
Updated 18-Dec-13 2:54am
v2
Comments
CHill60 18-Dec-13 8:55am    
Why are you requerying the data for you dataGridView1 every time you change the text in textBox1???
Vivek.anand34 18-Dec-13 9:00am    
Yes for eg.i buy many things from store.. one thing is entered in text tat cames to grid. but another thing enter in that text box below alredy entered thing is hided. but add 1-by-1 how many thing we buy in stores
CHill60 18-Dec-13 9:07am    
I didn't understand your reply. But here is a clue ... I can't think of any reason why you would need to requery for the datagridview every time a user types a character into a textbox. Rethink your design
Vivek.anand34 18-Dec-13 9:11am    
U go and buy some things in supermarket.. one product entered in text box that comes to grid. and another product enter in textbox tat also comes to grid.. tat ly i asking.. but above code cant add in grid jst changed wt am typed in text box..
Ganesh KP 18-Dec-13 9:06am    
what you are trying to achieve?

Execute your procedure in the Validated event handler, rather than in the TextChanged one; as you already have been told, recreating a DB connection and query everytime a new character is entered in a TextBox is a design issue, it will make your application less responsive and annoying, from a user experience point of view.

You should also avoid constructing SQL queries by concatenating values obtained from users; this leaves your code opened to SQL injection attacks.
For example, if anyone enters the following text in textBox1:
';DROP DATABASE ParamBills;--

then you're busted.

Regards.
 
Share this answer
 
You can do one thing.

Take one HiddenField and store the TextBox value in it inside the TextChanged Event.
Something like below...
C#
private void textBox1_TextChanged(object sender, EventArgs e)
{
    if(String.IsNullOrEmpty(hdnField.Value))
    {
        hdnField.Value = textBox1.Text;
    }
    else
    {
        hdnField.Value = hdnField.Value + "," + textBox1.Text;
    }

    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=192.168.1.4;Initial Catalog=ParamBills;Persist Security Info=True;User ID=sa;Password=login";
    conn.Open();
    SqlCommand cmd = new SqlCommand("select Product_Name,Product_Code,Item_Name,Item_code,qty from Master_Stock_Register where Product_Name IN (@TextBoxValue)", conn);
    command.Parameters.AddWithValue("@TextBoxValue", hdnField.Value);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill (dt);
    dataGridView1.DataSource = dt;
  // dataGridView1.DataBind();
    conn.Close();
}
 
Share this answer
 
v2

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