NEWBIE: How to test if OleDbDataReader is empty without reading it?

Posted on 2006-04-20
Last Modified: 2008-02-26
Dear Experts,

I have a method that accepts an OleDbDataReader, solely to determine if it's empty.  This was a problem with Java ResultSets, which I'm converting to C#.  You had to use a special "isAfterLast" method if you wanted to determine emptiness without actually reading from the ResultSet.  I see something called "hasRows()", but it doesn't seem to be an actual method of OleDbDataReader.  (I'm a TOTAL C# newbie...)

Question by:BrianMc1958
    LVL 12

    Expert Comment

    OleDbDataReader has a boolean property called HasRows
    LVL 5

    Accepted Solution

    You are on the right track. HasRows does exactly what you want. It returns a boolean value indicating whether the DataReader is empty (HasRows is false) or not (HasRows is true).

    hasRows() - as you wrote - won't work for two reasons: First, C# is case sensitive, you have to capitalize the "H" of HasRows. Second, HasRows is a property not a method. If you don't implement properties but just use them, you can think of them as normal object variables. So, you must not use parenthesis, just "HasRows":

    if (myDataReader.HasRows) {
      // do something

    Author Comment

    Excellent!  I love making the dumb mistakes you learn from!  I was just automatically putting in the "()" after HasRows...

    Thanks again!
    LVL 12

    Expert Comment

    lol, thanks wlfs, that will teach me to type in text before drinking my morning coffee!!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now