You are building a query, you will need to check every choice that is available to the user.
What you can do is build the query as you go.
The
Select
is always the same:
Dim QueryString = new StringBuilder()
Dim WhereBuilder = new List(Of String)()
QueryString.Append("select name, age from tbUser")
Then do each test:
If Not String.IsNullOrWhitespace(cboName.Text) Then
WhereBuilder.Add("name = '" + cboName.Text + "'")
End If
If Not String.IsNullOrWhitespace(cboAge.Text) Then
WhereBuilder.Add("age = '" + cboAge.Text + "'")
End If
Now you are ready to build the query:
If WhereBuilder.Count > 0 Then
QueryString.Append(" where ")
QueryString.Append(String.Join(" and ", WhereBuilder)
End If
dim query = QueryString.ToString()
NOTE: Code is written in the answer untested, so may need cleaning up.