943,763 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 621
  • C# RSS
Jun 25th, 2009
0

changing multiple lables with one sql query

Expand Post »
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.
c# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 27
Solved Threads: 0
Light Poster
Yellowdog428 is offline Offline
25 posts
since Mar 2008
Jun 25th, 2009
0

Re: changing multiple lables with one sql query

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

c# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jun 26th, 2009
0

Re: changing multiple lables with one sql query

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: date t date crystal report
Next Thread in C# Forum Timeline: datagrid in windows application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC