Ray
Ray

Reputation: 31

SELECT COUNT (*) returning -1

I need help with this one, when I run my query and put it in a variable it returns -1, anyone knows why?

OleDbCommand numberOfHomes = new OleDbCommand("SELECT COUNT(*) FROM HOME", con);

int homes = numberOfHomes.ExecuteNonQuery();

lblNumberOfHomes.Text = homes.ToString();

This is done in Visual Studio 2013

Thanks

Upvotes: 1

Views: 254

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460208

anyone knows why?

Because you use ExecuteNonQuery for a query. You want to use ExecuteScalar.

int homes = (int)numberOfHomes.ExecuteScalar();

From the ExecuteNonQuery-documentation:

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

Upvotes: 5

Related Questions