When you have opened the database and have your SELECT statement ready, use a
DataTable[
^] to hold all informations from the database.
sqlConnection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM tbl WHERE id=42", sqlConnection);
DataTable result = new DataTable();
dataAdapter.Fill(result);
sqlConnection.Close();
When
result
(DataTable) holds all rows that match the WHERE Clause.
Get value from
result
and populate TextBox.
if (result.Rows.Length > 0)
{
textBox_FirstName.Text = result["firstname"].ToString();
textBox_Surname.Text = result["surname"].ToString();
textBox_Dob.Text = result["dob"].ToString();
textBox_Gender.Text = result["gender"].ToString();
}
else
{
MessageBox.Show("No record found");
}
The DataTable
result
can be saved for future use.