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

Recommended Answers

All 2 Replies

Select * from tableName where primary key = '" + textBox1.Text + "';

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.