Click here to Skip to main content
15,886,088 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have SQL query that gives me a single data like below:


SELECT Count(Activity)
FROM [Activities]
where Activity = 7


It gives me 57 and I would like 57 to be displayed on my windows form using a label. How could I display that SQL data in my windows form using a label?
Posted

Using the SqlCommand object and the SqlConnection class.

Something similar to:

C#
SqlConnection conn = new SqlConnection();
... set conn properties
SqlCommand cmd = new SqlCommand();
... set cmd properties
cmd.CommandText = "SELECT Count(Activity) FROM [Activities] where Activity = 7
Object temp = cmd.ExecuteScalar();
label.Text = temp.ToString();


And add try,catch, etc.
 
Share this answer
 
Comments
fatihkaratay 30-Oct-13 18:45pm    
That'll work. Thank you!
ZurdoDev 30-Oct-13 19:09pm    
No problem.
Try this:
SqlConnection myConn = new SqlConnection("your connection");
                       myConn.Open();
                       SqlCommand myCommand = myConn.CreateCommand();

                           myCommand.CommandText = "Select Count(Activity)From [Activities] Where Activity = 7";
                           myCommand.CommandType = CommandType.Text;
                           SqlDataReader reader = myCommand.ExecuteReader();
                           DataTable dt2 = new DataTable();
                           dt2.Load(reader);

                           if (dt2.Rows.Count != 0)
                           {
                               yourLabel.Text = dt2[0].ToString();
                           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900