Microsoft KB Archive/308352

= No RecordCount property exists to indicate how many records are being fetched when you use the OleDbDataReader or SqlDataReader class in Visual C# =

Article ID: 308352

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft ADO.NET 2.0
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005

-



This article was previously published under Q308352





For a Microsoft Visual Basic .NET version of this article, see 308050.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient



SYMPTOMS
When you use the OleDbDataReader or SqlDataReader class, no RecordCount property exists to indicate how many records are being fetched.



CAUSE
The DataReader object or the back-end data source does not typically know how many records are being fetched until the last record is sent to the client.

Even an ActiveX Data Objects (ADO) Recordset returns -1 for the RecordCount property when it uses a forward-only cursor to retrieve data. DataReader exhibits similar behavior because it uses a forward-only cursor to retrieve rows and columns.



RESOLUTION
To work around this problem, use one of the following methods:
 * Count the records as you go through the reader.
 * Run a SELECT COUNT(*) query first. Note that this query may be out of date by the time you finish reading the data.



STATUS
This behavior is by design.



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. Create a new Windows Application in Visual C# .NET. Form1 is created by default. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not. Add a Command button to Form1, and change its Name property and its Text property to btnTest .</li>  Use the using statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. using System; using System.Data; using System.Data.SqlClient; </li>  Return to Form view, and double-click btnTest to add the click event handler. Add the following code to the handler: String myConnString = &quot;User ID=sa;password=sa;Initial Catalog=pubs;Data Source=mySQLServer&quot;;

String mySelectQuery = &quot;SELECT * FROM Authors&quot;; SqlConnection myConnection = new SqlConnection(myConnString); SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection); myConnection.Open;

SqlDataReader myReader ; myReader = myCommand.ExecuteReader; int RecordCount = 0;

try {   while (myReader.Read) {       RecordCount++; }   if (RecordCount == 0) MessageBox.Show(&quot;No data returned&quot;); else MessageBox.Show(&quot;Number of Records returned: &quot; + RecordCount); }   catch (Exception ex) {   MessageBox.Show(ex.ToString); }       finally {   myReader.Close; myConnection.Close; }                   </li> Modify the connection string (myConnString) as appropriate for your environment.</li> Save your project.</li> On the Debug menu, click Start to run your project.</li> Click on the button. Notice that you can see the record count.</li></ol>

<div class="references_section">