I have 5 Dropdownlist/Comboboxes. Is there a more efficient way to bind the data using a function and parameter where in some cases Parameter can be null. Instead of creating an individual databind for each scenario.
I want the user to be able to see the data in the repeater based on one or more (combinations) of dropdownlist selections. Eg. user selects value in DdlA and in ComboboxA but wants to leave the others blank.
below is a sample of code I have wrote which works for only one selection at a time. how can I tweak it to use multiple but not necessarily all selected values.
Protected Sub BttnSave_Click(sender As Object, e As EventArgs) Handles BttnSave.Click
If DdlA.SelectedValue = "1" And DdlB.SelectedValue = "1" And DdlC.SelectedValue = "1" And ComboboxA.SelectedValue = "" And DdlD.Text = "" Then
Me.ReadAllData()
ElseIf DdlA.SelectedValue <> "1" And DdlB.SelectedValue = "1" And DdlC.SelectedValue = "1" And ComboboxA.SelectedValue = "" And DdlD.Text = "" Then
Me.ReadAData()
ElseIf DdlA.SelectedValue = "1" And DdlB.SelectedValue <> "1" And DdlC.SelectedValue = "1" And ComboboxA.SelectedValue = "" And DdlD.Text = "" Then
ReadBData()
ElseIf DdlA.SelectedValue = "1" And DdlB.SelectedValue = "1" And DdlC.SelectedValue <> "1" And ComboboxA.SelectedValue = "" And DdlD.Text = "" Then
ReadCData()
ElseIf DdlA.SelectedValue = "1" And DdlB.SelectedValue = "1" And DdlC.SelectedValue = "1" And ComboboxA.SelectedValue <> "" And DdlD.Text = "" Then
ReadComboboxData()
ElseIf DdlA.SelectedValue = "1" And DdlB.SelectedValue = "1" And DdlC.SelectedValue = "1" And ComboboxA.SelectedValue = "" And DdlD.Text <> "" Then
ReadDData()
End If
End Sub
Private Sub ReadAData()
Dim query As String = "SELECT * FROM Test WHERE A = @A ORDER BY ID DESC"
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@A", DdlA.SelectedItem.Text)
Using con As SqlConnection = New SqlConnection(GblSqlCon)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Repeater1.DataSource = dt
Repeater1.DataBind()
End Using
End Using
End Using
End Sub
Private Sub ReadBData()
Dim query As String = "SELECT * FROM [dbo].[TestActivityLog] WHERE B = @B ORDER BY ID DESC"
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@B", DdlB.SelectedItem.Text)
Using con As SqlConnection = New SqlConnection(GblSqlCon)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Repeater1.DataSource = dt
Repeater1.DataBind()
End Using
End Using
End Using
End Sub
2条答案
按热度按时间cld4siwp1#
If you want all or "some" of the combo box to be optional for the search criteria , then you do NOT need to handle all combinations.
The simple way is to look at a combo box, if it has a value, then you filter, if the combo box is empty, then you ignore it.
This approach works for 3 or 15 optional filter box(s).
Many don't realize that the SQL command object allows adding as many parameters as you want, and the final SQL does NOT have to be defined WHILE you add these parameters.
So the design pattern is:
Check if control has value - if yes, then add to sql, and add to parameter.
Keep going until done.
As you can see, then you don't have to deal with all of the combinations of the filter options - you just ignore ones without a value.
I don't have your data, but lets do a proof of concept.
I have a list of hotels.
Now, recalling high school math (combinations and permutations), the above is 6! (6 factorial) and represents 720 combinations!!!
We can't possible code that out!!!
However, WE STILL want sql inject safe code (that means parameter).
So, we have this markup:
So, ALL of the filter options are "optional"
The code pattern thus is SAME as first example, and looks like this:
So, code to load combox (not really important - but a good FYI!!!)
So, the filter code is quite simple code, this:
In fact MUCH of the above is cut + paste over and over.
And now we see/get this:
pgccezyw2#
EDITED
Try using StoredProcedures.
If I am not wrong you want to create something like if any combobox value is selected then your repeater should load up.
Check this following method.
To Create SQL Server Stored Procedure,
If you have a column with a fixed value in your table
TestActivityLog
then you can avoid type1 and use it something like thisI hope this helps you. :)