Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i write a webservice.
in the cs file i write bellow lines. after run, it returns 1.
but there are many rows in mytable.


SqlConnection con = new SqlConnection("Data Source=mypc;Initial Catalog=myDB;User ID=admin;Password=admin123");
    SqlCommand cmd;
    SqlDataReader dr;
    DatatableToJson dts = new DatatableToJson();
    long id = 0;
    string Table_Name = "";


    [WebMethod]
    public void OnlineUsers_Counts()
    {
        DataTable dt = new DataTable();
        SqlCommand myCMD = new SqlCommand();
        myCMD.CommandText = "select count(*) from OnlineUsers  ";  
        
        con.Close();
        con.Open();

        myCMD.Connection = con;
        dr = myCMD.ExecuteReader();

        dt.Clear();
        dt.Load(dr);
                
        if (dt.Rows.Count > 0)
        {
            Context.Response.Write(dt.Rows.Count.ToString());
        }


thanks

What I have tried:

i couldn't do any things for solve the problems.


help
Posted
Updated 9-Nov-18 11:35am

Um. You get 1, don't you?

That's because
SQL
SELECT COUNT(*) FROM MyTable
Returns a single row, with a single field in, which contains the number of rows in the original table...

I'd use ExecuteScalar which returns a single value - the number of rows - instead of ExecuteReader.
 
Share this answer
 
SQL
select count(*) from OnlineUsers
The ExecuteReader() method is going to retrieve this data as a table. There will be one row with one field.

What you can do is to change the method; use ExecuteScalar();. The needed code is a few lines shorter as well.
C#
        int RowCount = myCMD.ExecuteScalar();
        Context.Response.Write(RowCount);
//      dr = myCMD.ExecuteReader();
//
//      dt.Clear();
//      dt.Load(dr);
                
//      if (dt.Rows.Count > 0)
//      {
//            Context.Response.Write(dt.Rows.Count.ToString());
//       }
 
Share this answer
 
If I may add to the solution provided, I would strongly recommend you do the following:

(1) Always put app level configuration such as your connection string in your web.config file so you won't have to recompile and redeploy your code when you modify your connection string.

(2) Wrap objects that eat resources such as SqlConenction and SqlCommand within a using block so objects will be automatically disposed after using them. Here's a quick example:

C#
[WebMethod]
public void OnlineUsers_Counts()
{
	int rowCount = 0;
        using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
           using(SqlCommand cmd = new SqlCommand("SELECT count(*) FROM OnlineUsers" , connection)){
               	 cmd.CommandType = CommandType.Text;
                 rowCount = cmd.ExecuteScalar();
        	
        }
        
	Context.Response.Write(rowCount);
}
 
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