I am trying to figure out how to set multiple text boxes/labels with one sql query.

I dont need help connecting to the database or anything like that I just need to know how I can write one query to and set multiple text boxes with that one query.

here is what I have been doing, and there has got to be an easier way.

string selectAddress = "select cust_address from customers where customer_id = " + customerID;
string selectCity = "select customer_city from customer where customer_id = " + customerID;

SqlCommand cmdAddress = new SqlCommand(selectAddress, con);
SqlCommand cmdCity = new SqlCommand(selectCity, con);

this.lblAddress1.Text = cmdAddress.ExecuteScalar().ToString();
this.lblCity.Text = cmdCity.ExecuteScalar().ToString();

I would like to be able to write one query (actually just put it in a procedure) and set the text boxes/labels to the results of the query.

Thanks in advance for any help!
Cheers

Instead of calling ExecuteScalar you should open a DataReader and populate a DataTable.

private void button1_Click(object sender, EventArgs e)
    {
      const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
      const string query = "Select * From Invoice Where InvNumber = @InvNumber";
      using (DataTable dt = new DataTable())
      {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
          conn.Open();
          using (SqlCommand cmd = new SqlCommand(query, conn))
          {
            cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100));
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
              dt.Load(dr);
            }
          }
          conn.Close();
        }
        if (dt.Rows.Count != 1)
          throw new Exception("Row not found");

        DataRow row = dt.Rows[0];
        string invNumber = Convert.ToString(row["InvNumber"]);
        string custName = Convert.ToString(row["CustomerName"]);

        System.Diagnostics.Debugger.Break();
      }
    }
DataTable tbl = new DataTable();
SqlDataAdapter adapt = new SqlDataAdapter(string.Format("select cust_address,customer_city  from customers where customer_id = {0}", customerID), con);
adapt.Fill(tbl);
if (tbl.Rows.Count > 0)
{
       lblAddress1.Text = (string)tbl.Rows[0]["cust_address"];
       lblCity.Text = (string)tbl.Rows[0]["customer_city"];
}
tbl.Dispose();

Just one of many ways of doing this.

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.