How to find the record count for a DataReader in ASP.NET?

Question

How to find the record count for a DataReader in ASP.NET?

Re: How to find the record count for a DataReader in ASP.NET?

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