954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to ignore null filter criteria

Hi all!

I try to filter a datagridview and I wrote an sql statement for that reason. The problem is that although I achieved to have multifilter I can find how to make my cod ignore the criteria which are null (empty criteria, when the user doesn t give any values). I tried the code below but I know that it isn t correct.

"Select * from INCIDENTS where (DMA =Is NULL OR DMA='" & txtDMA.Text &'") AND (StreetName IS NULL or StreetName LIKE '" & txt.text & "%' )"

Plz help!

skran
Light Poster
43 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
 

You could build the query like

Dim query As String = "select * from Incidents"

If txtDMA.Text <> "" Or txtStreet.Text <> "" Then
    query &= " where "
    If txtDMA.Text <> "" Then
        query &= "DMA = '" & txtDMA.Text & "'"
    End If
    If txtStreet.Text <> "" Then
        If txtDMA.Text <> "" Then
            query &= " AND"
        End If
        query &= " StreetName LIKE '%" & txtStreet.Text & "%'"
    End If
End If
Reverend Jim
Posting Shark
Moderator
1,167 posts since Aug 2010
Reputation Points: 253
Solved Threads: 159
 

thanks a lot!

skran
Light Poster
43 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: