| | |
changing multiple lables with one sql query
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 22
Reputation:
Solved Threads: 0
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.
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
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)
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.
c# Syntax (Toggle Plain Text)
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(); } }
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
C# Syntax (Toggle Plain Text)
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();
![]() |
Similar Threads
- how to delete duplicate record in a table by using SQL query (MS SQL)
- SQL Query not quite pulling the data I expected... (MySQL)
- help in sql query (PHP)
- Read multiple column data from SQL query ( 1 row ) (ASP.NET)
- Javascript array from sql query (JSP)
- Please help me out with MySQL query (MySQL)
- PHP/SQL query help (PHP)
- Retreiving variables from a sql query into a form (PHP)
Other Threads in the C# Forum
- Previous Thread: date t date crystal report
- Next Thread: datagrid in windows application
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml






