Get number of rows in a SQL Server table in VB.NET

ado.net, sql-server, vb.net

Solution

The solution is to replace

count = cmd.ExecuteNonQuery

with

count = cmd.ExecuteScalar 

Like Robert Beaubien said in his comments

Problem

There are 10 rows in `primary_student_table`. When I execute the following code, the result was -1. ``` Dim count As Int16 con.Open() query = "SELECT COUNT(roll) AS rollcount FROM primary_student_table WHERE admityear = 2011 AND batch = 1 " cmd = New SqlCommand(query, con) count = cmd.ExecuteNonQuery MsgBox(count) con.Close() ``` What's the problem in the above code?

Original source