0

how to automatically display data in textbox from database when primary key is added in textbox

3
Contributors
2
Replies
5
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
0

Yes, to extend Chris`s code:

//in TextChanged event hander of textBox1:
//
string myID = Convert.ToInt32(textBox1.Text.Trim());
SqlConnection conn = new SqlConnection("connString");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"SELECT Column1, Column2 FROM MyTable WHERE ColumnID = @param";
cmd.Parameters.Add("@param", SqlDbType.Int).Value = myID;
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{
    textBox2.Text = (string)reader[0];
    textBox3.Text = (string)reader[1];
}
else
{
    textBox1.Text = "";
    textBox1.Text = "";
}
reader.Dispose();
cmd.Dispose();
conn.Dispose();

As you can see this example includes 3 textBoxes. 1st one is for inserting IDs, other two are for inserting data read from database.
Subscribe to TextChanged event of textBox1 control, and put this code inside of it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.