2016-06-26 9 views
0

Ich habe Webapplikation, wo es 1000 von Produkten gibt, so gibt es mehrere Filter muss da sein. Ich habe insgesamt 4 checkboxList. Jetzt ist mein Problem, wenn ich Filter von irgendeinem von checkboxList anwende, dann prüft es ausgewählten Wert für alle Spalte, die in der Abfrage spezifiziert wird. Was ich will, ist wie untenAnwenden von Filtern aus mehreren checkboxList zum Filtern von ListView in VB.net

SELECT * FROM products WHERE price_rang IN ('selectedValueFromCheckBoxList1') And category IN ('selectedValueFromCheckBoxList2') 

Was jetzt

SELECT * FROM `products` WHERE price_range IN ('selectedValueFromCheckBoxList2') AND category IN ('selectedValueFromCheckBoxList2') 

in dieser Abfrage So geschieht nehme an, wenn ich Wert auswählen Erste aus checkboxList2 dann für beide Spalten es diesen Wert & Ergebnis nimmt nicht angezeigt .

Unten ist mein Filterprozess Code

Private Sub getResult() 
     Dim constr As String = ConfigurationManager.ConnectionStrings("conio").ConnectionString 
     Dim query As String = "select * from products" 

     Dim condition As String = String.Empty 
     For Each price As ListItem In priceFilter.Items 
      condition += If(price.Selected, String.Format("'{0}',", price.Value), String.Empty) 
     Next 

     For Each sub_category As ListItem In category.Items 
      condition += If(sub_category.Selected, String.Format("'{0}',", sub_category.Value), String.Empty) 
     Next 

     If Not String.IsNullOrEmpty(condition) Then 
      condition = String.Format(" WHERE price_range IN ({0}) and sub_category IN ({0})", condition.Substring(0, condition.Length - 1)) 
     End If 

     Using con As New MySqlConnection(constr) 
      Using cmd As New MySqlCommand(query & condition) 
       Using sda As New MySqlDataAdapter(cmd) 
        cmd.Connection = con 
        Using dt As New DataTable() 
         sda.Fill(dt) 
         products.DataSource = dt 
         products.DataBind() 
        End Using 
       End Using 
      End Using 
     End Using 
    End Sub 
+0

Ihr "was ich will" und Ihr "was jetzt passiert" sehen für mich gleich aus. Ist es ein Tippfehler? –

+0

@AndrewMortimer sehen Was ich in dieser ID von checkboxList will, wird anders sein. & was jetzt passiert, in dem beide Spalten Werte von der gleichen checkboxList erhalten – SUN

+0

@AndrewMortimer angenommen, dass ich price_range Filter nicht angewandt & direkt auf Kategorie dann abgefragt habe, wird Abfrage wie dieses gemacht AUSWÄHLEN * VON PRODUKTEN WHERE price_range IN ('Energienbank') AND category IN ('power bank') aber im Idealfall, wenn der Preisfilter nicht angewendet wird, sollte er leer bleiben bis zu dem Zeitpunkt, an dem er nicht angewendet wurde. – SUN

Antwort

1

Dies ist eine Option ist, dass Sie in den markierten Produkte nur interessiert sind.

Public Function buildWhereClause() As String 

     Dim query As String = "select * from products" 
     Dim joiner As String = " " 

     Dim condition As String = String.Empty 
     Dim priceCondition As String = String.Empty 

     For i = 0 To priceFilter.Items.Count - 1 

      If priceFilter.Items(i).Selected Then 
       Dim price As String = priceFilter.Items(i).ToString 
       priceCondition = String.Concat(priceCondition, joiner, String.Format("'{0}'", price)) 
       If joiner = " " Then joiner = ", " 
      End If 
     Next 

     Dim categoryCondition As String = String.Empty 
     joiner = " " 

     For i = 0 To categoryFilter.Items.Count - 1 
      If categoryFilter.Items(i).Selected Then 
       Dim category As String = categoryFilter.Items(i).ToString 
       categoryCondition = String.Concat(categoryCondition, joiner, String.Format("'{0}'", category)) 
       If joiner = " " Then joiner = ", " 
      End If 
     Next 

     Dim whereClause As String = String.Empty 
     joiner = " where " 
     If Not String.IsNullOrEmpty(priceCondition) Then 
      whereClause = String.Concat(whereClause, joiner, String.Format(" price_range IN ({0})", priceCondition)) ' and sub_category IN ({0})", condition.Substring(0, condition.Length - 1)) 
      joiner = " and " 
     End If 

     If Not String.IsNullOrEmpty(categoryCondition) Then 
      whereClause = String.Concat(whereClause, joiner, String.Format(" sub_category in ({0})", categoryCondition)) 
      joiner = " and " 
     End If 

     Return String.Concat(query, whereClause) 

    End Function 
+0

In meinem codeOnSelectedIndexChanged versuchte ich sub aufzurufen, das nicht mit dem angegebenen Code arbeitet, daher wird nichts beeinflusst – SUN

+0

Versuchen Sie zu filtern, wenn sich die Auswahl ändert? Oder auf allen überprüften Artikeln in der Liste? –

+0

onSelectedIndexChanged Ich möchte filtern – SUN