changing multiple lables with one sql query

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 22
Reputation: Yellowdog428 is an unknown quantity at this point 
Solved Threads: 0
Yellowdog428 Yellowdog428 is offline Offline
Newbie Poster

changing multiple lables with one sql query

 
0
  #1
Jun 25th, 2009
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.
  1. string selectAddress = "select cust_address from customers where customer_id = " + customerID;
  2. string selectCity = "select customer_city from customer where customer_id = " + customerID;
  3.  
  4. SqlCommand cmdAddress = new SqlCommand(selectAddress, con);
  5. SqlCommand cmdCity = new SqlCommand(selectCity, con);
  6.  
  7. this.lblAddress1.Text = cmdAddress.ExecuteScalar().ToString();
  8. 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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,200
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: changing multiple lables with one sql query

 
0
  #2
Jun 25th, 2009
Instead of calling ExecuteScalar you should open a DataReader and populate a DataTable.

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where InvNumber = @InvNumber";
  5. using (DataTable dt = new DataTable())
  6. {
  7. using (SqlConnection conn = new SqlConnection(connStr))
  8. {
  9. conn.Open();
  10. using (SqlCommand cmd = new SqlCommand(query, conn))
  11. {
  12. cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100));
  13. using (SqlDataReader dr = cmd.ExecuteReader())
  14. {
  15. dt.Load(dr);
  16. }
  17. }
  18. conn.Close();
  19. }
  20. if (dt.Rows.Count != 1)
  21. throw new Exception("Row not found");
  22.  
  23. DataRow row = dt.Rows[0];
  24. string invNumber = Convert.ToString(row["InvNumber"]);
  25. string custName = Convert.ToString(row["CustomerName"]);
  26.  
  27. System.Diagnostics.Debugger.Break();
  28. }
  29. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: changing multiple lables with one sql query

 
0
  #3
Jun 26th, 2009
  1. DataTable tbl = new DataTable();
  2. SqlDataAdapter adapt = new SqlDataAdapter(string.Format("select cust_address,customer_city from customers where customer_id = {0}", customerID), con);
  3. adapt.Fill(tbl);
  4. if (tbl.Rows.Count > 0)
  5. {
  6. lblAddress1.Text = (string)tbl.Rows[0]["cust_address"];
  7. lblCity.Text = (string)tbl.Rows[0]["customer_city"];
  8. }
  9. tbl.Dispose();
Just one of many ways of doing this.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC