h1

Count records using ExecuteScalar in ASP.NET

April 15, 2008

After much research, it was incredibly annoying to try and find a decent script on how to count records from a database using ASP.NET (via VB). I gathered from different resources and wrote a simple script to get the job done. I decided to post it in case anyone is going through the same headache I just went through.

You can put this in a function and return the record count easily. The following is a simple example of how to count records using ExcuteScalar in ASP.NET.

Dim SQLString As String = “SELECT Count(columnName) FROM tableName
Dim
yourDBConnection As New SqlConnection()
yourDBConnection.ConnectionString = yourConnectionString

Dim
cmd As New SqlCommand()
Dim recCount As Integer

yourDBConnection.Open()

cmd.Connection = yourDBConnection
cmd.CommandText = SQLString
recCount = Integer.Parse(cmd.ExecuteScalar().ToString)

yourDBConnection.Close()

‘Return recCount

Happy programming!

One comment

  1. Thx ..was a real help.:)



Leave a Comment