Returning a single value from SQL Server query to a C# label

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 10
Reputation: history84084 is an unknown quantity at this point 
Solved Threads: 0
history84084 history84084 is offline Offline
Newbie Poster

Returning a single value from SQL Server query to a C# label

 
0
  #1
Aug 19th, 2009
I am trying to populate a label using a T-Sql query to a SQL Server table upon a click event. I have the click event and linking to the SQL Server table working just fine. The process (below) returns "System.Windows.Forms.BindingSource" rather than any value from the table. My code is as follows.
  1. string strCon = "Data Source=ITxxx;Initial Catalog=FDM;Integrated Security=True";
  2. // Mailed date
  3. string strSQLi = "select " +
  4. "Mailed_Dt " +
  5. "from [FDM].dbo.XREF_ProductTbl" +
  6. " where Product = " + " '" + cboxMailRespSelProgram.Text.ToString() + "'";
  7. SqlDataAdapter dataAdapterI = new SqlDataAdapter(strSQLi, strCon);
  8. SqlCommandBuilder commandBuilderI = new SqlCommandBuilder(dataAdapterI);
  9. DataTable tblProductI = new DataTable();
  10. BindingSource productIBindSource = new BindingSource();
  11. productIBindSource.DataSource = tblProductI;
  12. dataAdapterI.Fill(tblProductI);
  13. lblProductI.Text = "Mailed Date: " + productIBindSource;

Your help will be greatly appreciated!
Last edited by John A; Aug 20th, 2009 at 10:27 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 500
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Returning a single value from SQL Server query to a C# label

 
0
  #2
Aug 19th, 2009
>Returning a single value from SQL Server query to a C# label,
Use ExecuteScalar method of SqlCommand class.
  1. ...
  2. object val=cmd.ExecuteScalar();
  3. ...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,406
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: 613
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Returning a single value from SQL Server query to a C# label

 
0
  #3
Aug 20th, 2009
Using data adapters, command builders, etc is a little bit of overkill for the task at hand. As adatapost suggested you will want to use .ExecuteScalar() . Also keep in mind if the query may ever grow you will want to use a DataTable .

  1. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  2. const string query = "Select Password From UserTable (NOLOCK) Where UserName = @UserName";
  3. DataTable result = new DataTable();
  4. using (SqlConnection conn = new SqlConnection(connStr))
  5. {
  6. conn.Open();
  7. using (SqlCommand cmd = new SqlCommand(query, conn))
  8. {
  9. cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
  10. using (SqlDataReader dr = cmd.ExecuteReader())
  11. {
  12. result.Load(dr);
  13. string textBoxStuff = Convert.ToString(result.Rows[0]["Column"]);
  14. }
  15. }
  16. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: history84084 is an unknown quantity at this point 
Solved Threads: 0
history84084 history84084 is offline Offline
Newbie Poster

Re: Returning a single value from SQL Server query to a C# label

 
0
  #4
Aug 20th, 2009
Both responses were very helpful. Thank you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC