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

Try
     conn = New SqlConnection(“your connection string goes here”)
     conn.Open()
     cmd = New SqlCommand(“your SQL statement”, conn)
     dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
     If True = dr.Read Then
          Response.Write(dr(“Column1”))
          While dr.Read
               Response.Write(dr(“Column1”))
          End While
     Else
          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)

Finally
     If Not dr Is Nothing Then
          dr.Close()
     End If
     conn.Close()
End Try

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: