Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering Datatable/view

Status
Not open for further replies.

jbtman

Programmer
Jul 13, 2007
30
I have a funciton that I am trying to use to filter a datatable of customer records but it always returns all rows.

Am I doing something wrong? Any help appreciated!!

Code:
Public Function CustomerLookup(ByVal strFirstName As String, ByVal strLastName As String) As DataTable
        Dim dtTemp As DataTable
        Dim dv As DataView

        dtTemp = GetAllCustomers()
        dv = New DataView(dtTemp)

        If strFirstName = vbNullString And strLastName <> vbNullString Then
            dtTemp.Select("LastName LIKE '" & strLastName & "%'")
        ElseIf strFirstName <> vbNullString And strLastName = vbNullString Then
            dv.RowFilter = "FirstName LIKE '" & strFirstName & "%'"
        Else
            dtTemp.Select("FirstName LIKE '%" & strFirstName & "' AND LastName LIKE '%" & strLastName & "'")
        End If

        dtTemp = dv.Table.Copy

        Return dtTemp
    End Function
 
What if a firstname is null and a lastname is not, or vice-versa?.... the first 2 conditions will always be false. Furthermore, if either the firstname or lastname is null, the like in the third condition will be looking for everything.

Kevin Davie
Consultant
Sogeti USA
 
Oh I realize that it is not complete. I am just testing something and the elseif condition is always met. But won't work for some reason!
 
The problem is you are doing a .select on the datatable and not doing anything with the results. A .select on a datatable returns an array of datarow objects. You need to do:
Code:
'Example
dim dr() as datarow
dr =  dtTemp.Select("FirstName LIKE '%" & strFirstName & "' AND LastName LIKE '%" & strLastName & "'")


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top