Parag’s Den

Dedicated to my adored friends…..

Asp.Net (How to find the record count for a DataReader?)

Posted by paragdongre on November 6, 2007

There is no methods or properties available for the DataReader object to find whether it has any records or not. The following code snippet can be used to when you handle with DataReaders.

Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader

     conn = New SqlConnection(“your connection string goes here”)
     cmd = New SqlCommand(“your SQL statement”, conn)
     dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
     If True = dr.Read Then
          While dr.Read
          End While
          Response.Write(“0 records found!”)
     End If

Catch exc As SqlException
     Response.Write(“SQL Error Occured: ” & exc.ToString)

Catch exc As Exception
     Response.Write(“Error Occured: ” & exc.ToString)

     If Not dr Is Nothing Then
     End If
End Try


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: