Q:

Searching a datatable by checking two columns

I am working on a search process that was identified at this link....
Simplest/fastest way to check if value exists in DataTable in VB.net?
I would like to use this search technique, but was wondering if there is any way to add two columns to the process....
Shared Function CheckValue(myTable As DataTable, columnName As String, searchValue As String) As Boolean
For row As DataRow In myTable.Rows
    If row(columnName) = searchValue Then Return True
    Next
    Return False
End Function

I cannot use a PK but would like to match on two fields one a string and the other is an integer.
thanks,

A:

If you want to use that method to search for 2 values in 2 columns, then:
Shared Function CheckValue(myTable As DataTable, columnName1 As String, searchValue1 As String, columnName2 As String, searchValue2 As Integer) As Boolean
  For row As DataRow In myTable.Rows
    If row(columnName1) = searchValue1 And row(columnName2) = searchValue2 Then Return True
  Next
  Return False
End Function

Please note, the above does not check for DBNull values though. If it encounters one, exception will be thrown. In order to cater for DBNull, the if statement needs to be modified.

