aleczandru
aleczandru

Reputation: 5459

Getting back number of rows in table returns -1

Hi I am tryng to count the number of rows in a table but I alwyas get back -1.Here is my code:

using (connection = new SqlConnection(connectionString))
{
      connection.Open();
      SqlCommand command = new SqlCommand("CountBooks", connection);
      command.CommandType = CommandType.StoredProcedure;
      numberOfBooks = command.ExecuteNonQuery();
}

And here is my query:

SELECT COUNT(*) FROM Books

How can I get the correct result?

Upvotes: 1

Views: 66

Answers (1)

8192K
8192K

Reputation: 5290

Use ExecuteScalar instead of ExecuteNonQuery.

Upvotes: 5

Related Questions