Microsoft Knowledge Base Email Alertz

KBAlertz.com: When using the

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 308050 - Last Review: March 9, 2006 - Revision: 1.2

There is no RecordCount property when yo use the OleDbDataReader class or the SqlDataReader class in Visual Basic .NET

This article was previously published under Q308050
For a Microsoft Visual C# .NET version of this article, see 308352  (http://kbalertz.com/Feedback.aspx?kbNumber=308352/ ) .

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

On This Page

SYMPTOMS

When using the OleDbDataReader or SqlDataReader class, there is no RecordCount property to tell how many records are being fetched.

CAUSE

The DataReader object (or even the back-end data source) doesn't typically know how many records are being fetched until the last one is sent to the client. Even an ActiveX Data Objects (ADO) Recordset returns -1 for RecordCount when retrieving data using a forward-only cursor. DataReader exhibits similar behavior because it uses a forward-only cursor for retrieving rows and columns.

RESOLUTION

You can work around this problem in the following ways:
  • Count the records as you go through the reader.
  • Run a SELECT COUNT(*) query first (although this may be out of date by the time you finish reading data).

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Open Visual Studio .NET.
  2. Create a new Windows Application in Visual Basic .NET.
  3. Make sure that your project contains a reference to the System.Data namespace.
  4. Place a Command button on Form1 and change its Name property to btnTest.
  5. Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code.
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    					
  6. Paste the following code in the code window after the region "Windows Form Designer generated code":
    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Dim myConnString As String = _
            "User ID=sa;password=sa;Initial Catalog=pubs;Data Source=mySQLServer"
        Dim mySelectQuery As String = "SELECT * FROM Authors"
        Dim myConnection As New SqlConnection(myConnString)
        Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
        myConnection.Open()
    
        Dim myReader As SqlDataReader
        myReader = myCommand.ExecuteReader()
        Dim RecordCount As Integer
        Try
            While myReader.Read()
                RecordCount = RecordCount + 1
            End While
    
            If RecordCount = 0 then
                MessageBox.Show("No data returned")
            Else
                MessageBox.Show("Number of Records returned: " & RecordCount)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        Finally
            myReader.Close()
            myConnection.Close()
        End Try
    End Sub
    					
  7. Modify the Connection string (myConnString) as appropriate for your environment.
  8. Save your project. On the Debug menu, click Start, and run your project.
  9. Click the button and you can see the record count.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
194973  (http://kbalertz.com/Feedback.aspx?kbNumber=194973/EN-US/ ) PRB: ADO: Recordcount May Return -1

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft ADO.NET 2.0
Keywords: 
kbnofix kbprb kbsqlclient kbsystemdata KB308050
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please