| | |
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:
Solved Threads: 0
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.
Your help will be greatly appreciated!
C# Syntax (Toggle Plain Text)
string strCon = "Data Source=ITxxx;Initial Catalog=FDM;Integrated Security=True"; // Mailed date string strSQLi = "select " + "Mailed_Dt " + "from [FDM].dbo.XREF_ProductTbl" + " where Product = " + " '" + cboxMailRespSelProgram.Text.ToString() + "'"; SqlDataAdapter dataAdapterI = new SqlDataAdapter(strSQLi, strCon); SqlCommandBuilder commandBuilderI = new SqlCommandBuilder(dataAdapterI); DataTable tblProductI = new DataTable(); BindingSource productIBindSource = new BindingSource(); productIBindSource.DataSource = tblProductI; dataAdapterI.Fill(tblProductI); 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
>Returning a single value from SQL Server query to a C# label,
Use ExecuteScalar method of SqlCommand class.
Use ExecuteScalar method of SqlCommand class.
c# Syntax (Toggle Plain Text)
... object val=cmd.ExecuteScalar(); ...
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 . C# Syntax (Toggle Plain Text)
const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;"; const string query = "Select Password From UserTable (NOLOCK) Where UserName = @UserName"; DataTable result = new DataTable(); using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username; using (SqlDataReader dr = cmd.ExecuteReader()) { result.Load(dr); string textBoxStuff = Convert.ToString(result.Rows[0]["Column"]); } } }
![]() |
Similar Threads
- Sql server connection using code in VB.NET (VB.NET)
- GET SQL SERVER Database Backup Message details in VB.NET (VB.NET)
- Quotes in sql server query error.! (Visual Basic 4 / 5 / 6)
- SQL Server Developer, Manchester, UK (Software Development Job Offers)
- SQL Server Question... (ASP.NET)
- SQL Server DBA - " Hot " (Software Development Job Offers)
- SQL Server DBA (Software Development Job Offers)
- Connecting to a SQL Server Database using VB (VB.NET)
- Query conversion from Sybase to MS SQL Server 2000 (MS SQL)
Other Threads in the C# Forum
- Previous Thread: Load text from webpage
- Next Thread: treat a form like a context menu...
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access algorithm array barchart bitmap box buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






