Katy J
Katy J

Reputation: 205

Display Query Values in Individual Textboxes

How can I show SQL query results in text boxes, such that for a query with three answers, I want to show them in three text boxes? Can I use ExecuteScalar or Listbox or recordset? How can I do this? I think I should use a loop but how?

Upvotes: 2

Views: 948

Answers (1)

Glory Raj
Glory Raj

Reputation: 17691

If you are executing a SQL command that returns a result, such as executing a SELECT statement you will have to use a different method. The SqlCommand's ExecuteReader method returns a SqlDataReader object that contains all of the records retrieved after executing the SQL command.

try
{
  SqlDataReader dr;
  dbCon.Open();

 //write your select statement here.....
  dr = sqlcom.ExecuteReader();
  if(dr.HasRows == True)
  {
    txt_clientID.Text = ((Integer) dr["cID"]).ToString();
    txt_clientAddress.Text = (String) dr["cAddress"];
    txt_clientPhoneNumber.Text = (String) dr["cPhoneNumber"];
  }
  dr.Close();
  dbCon.Close();
}
catch(Exception ex)
{} 

Upvotes: 1

Related Questions