Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I use the following code to count the number of rows in a table in MSSQL 2005 DB.
But, how should I write the C# code to get the number?


SQL
SqlConnection objcon = new SqlConnection();
            objcon.ConnectionString = "Data source=localhost; Initial catalog=BRIDGES_DB; User ID=sa; password=123;";
            SqlCommand objcom = new SqlCommand();
            objcom.CommandType = CommandType.Text;
            objcom.CommandText = "SELECT COUNT(marks_org) FROM activity_result";
            objcom.Connection = objcon;
            objcon.Open();
            if (dr.Read())
            {

               Label1.Text=?

            }
            objcon.Close();
Posted
Updated 1-Jun-12 6:57am
v2
Comments
Nelek 1-Jun-12 12:57pm    
Fixed code tags

1 solution

You would find the value by doing the following:

C#
Label1.Text = dr[0].ToString();


However, I don't see dr defined anywhere. Make sure you are doing the following:

C#
SqlDataReader dr = objcom.ExecuteReader();


before you do the if statement.

Here is a working tutorial that will point you in the right direction:
http://www.akadia.com/services/dotnet_data_reader.html[^]
 
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