Check for Value in DataReader

Posted on 2005-04-21
Last Modified: 2012-05-07
Need a way to check to see if a value exists in a datareader?
Question by:tbaseflug
    LVL 28

    Expert Comment

    you can execute datareader and then

        If  dr.HasRows Then

    'do something

    Expert Comment

    The problem here is that your reader will be consumed. And closed in my example. There is check for the existence of a value that I know of without consuming the reader. The DataReader is a read only forward only look at the date. Condider converting the reader to a DataTable first.

    public bool CheckReaderForValue(SqlDataReader Reader, string ColumnName, object Val)
                      if(Reader[ColumnName] == Val)
                            return true;
    LVL 35

    Accepted Solution

    you could loop through the DataReader and check every field:

    call this function and pass it an open DataReader and the value you want to search for. it will return true if the value was found and false otherwise
    Function FoundValue(ByVal Reader as OleDbDatareader, ByVal myValue as String) as Boolean
          Dim bFound as boolean
          While Reader.Read
             if Reader("Field1")=myValue then
              end if
          if Reader("Field2")=myValue then
              end if

    End While


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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