Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27508

VS 2010 [RESOLVED] Building a Where Clause with AND/OR

$
0
0
I am trying to make a filter system for a gridview and I have a DDL that gets column names from my database, and a text box that the user enters values into. You then click add to put it in the list box in a where clause format.
Attachment 96247
The problem I am having, is if I have 2 states I want to filter by, it adds "State = 'FL' AND State = 'CA'", which we know does not work because it can't equal both. I need to find a way to justify if the State column has already been used in the where clause/listbox.

Here is code:

vb .net Code:
  1. 'Adding the filters to DDL
  2.     Protected Sub btnAddFilter_Click(sender As Object, e As System.EventArgs) Handles btnAddFilter.Click
  3.         lbFilters.Items.Add(ddlSearch.Text & " = '" & txtFor.Text & "'")
  4.     End Sub
  5. 'Building them to where clause
  6.     Public Function buildSearch() As String
  7.         For i = 0 To lbFilters.Items.Count - 1
  8.             filterString = filterString + lbFilters.Items(i).ToString + " AND "
  9.         Next
  10.         Return filterString
  11.     End Function
  12. 'Creating query string and filtering
  13.      Public Sub Search()
  14.         Dim searchString As String = "SELECT Control_Number, Status, OrderNumber, State, County, Address, Date_Received, Priority, Client_Name From orders, client WHERE "
  15.         Dim listString As String = "SELECT Control_Number From orders WHERE "
  16.         searchString = searchString + buildSearch()
  17.         listString = listString + buildSearch()
  18.         Session("SelectCommand") = searchString & "orders.Client = client.ClientID ORDER BY Control_Number"
  19.         SqlDataSourceDelete.SelectCommand = Session("SelectCommand")
  20.         Session("OrderList") = Left(listString, listString.Length - 5)
  21.     End Sub

I don't think the sub Search() will be of any interest to this issue, but I put it there just incase.
Attached Images
 

Viewing all articles
Browse latest Browse all 27508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>