• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

Check for Value in DataReader

Need a way to check to see if a value exists in a datareader?
0
tbaseflug
Asked:
tbaseflug
1 Solution
 
iboutchkineCommented:
you can execute datareader and then

    If  dr.HasRows Then

'do something
0
 
davidhunterCommented:
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.HasRows)
      {
            while(Reader.Read())
            {      
                  if(Reader[ColumnName] == Val)
                  {
                        return true;
                  }
            }
      }
      Reader.Close();
}
0
 
YZlatCommented:
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
      bFound=false
      While Reader.Read
         if Reader("Field1")=myValue then
            bFound=true
          end if
      if Reader("Field2")=myValue then
            bFound=true
          end if

      ......
      FoundValue=bFound
End While


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now