Click to See Complete Forum and Search --> : Recordcount DataSet???


Joe Keller
October 29th, 2002, 08:54 AM
Im new to trying to use ADO.NET but have had alot of previous experience with standard ADO.

How do you get a recordcount for a dataset?

This is what i am doing now.. but I know there has to be a better way.


Private Function MaxRecords() As Integer
Dim sqlSelect As String
sqlSelect = "Select [Question] From [tblQuestions]"
Dim con As New OleDbConnection(ConnectionString)
Dim cmd As New OleDbCommand(sqlSelect, con)
Dim reader As OleDbDataReader
Try
con.Open()
reader = cmd.ExecuteReader()
MaxRecords = 0
Do While reader.Read()
MaxRecords = MaxRecords + 1
Loop
reader.Close()
Catch err As Exception
lblQuestion.Text = "Error reading survey data. "
lblQuestion.Text &= err.Message & " "
Finally
If (Not con Is Nothing) Then
con.Close()
End If
End Try
End Function

mhuguet
October 31st, 2002, 09:52 PM
Your not using a DataSet in this example. You cannot get a RecordCount from a DataReader. The DataReader is a connected Forward Only object. You can iterate through all of the records and have a counter variable to maintain the count.

You can get the count from a table in a DataSet. Remember a DataSet is a container of Tables which are containers of Rows.

MyRecCount = MyDataSet.Tables(0).Rows.Count