jamesbcox1980
asked on
ASP.Net: Create SELECT from ListBox with multiple selections
<asp:ListBox ID="selSearchProduct" CssClass="narrow_listbox" Font-Name="monospace" Font-Size="8" AppendDataBoundItems="true" runat="server" SelectionMode="Multiple" Rows="5">
</asp:ListBox>
I'm trying to modify an existing search script in a VB file that concatenates the WHERE portion of a SELECT statement based on the values of fields on the search form. Currently, the product field is a text box, but the users want the ability to search for multiple products.
The previous code looked like this:
If (Me.selSearchApplicationType.SelectedValue.Length > 0) Then
If (sWhere.Length > 0) Then sWhere &= " AND "
sWhere &= "application like @application"
Me.sqlGridSource.SelectParameters.Add("application", "%" & Me.selSearchApplicationType.SelectedValue & "%")
End If
If (Me.selSearchProduct.SelectedValue.Length > 0) Then
If (sWhere.Length > 0) Then sWhere &= " AND "
sWhere &= "product_id like @product"
Me.sqlGridSource.SelectParameters.Add("product", "%" & Me.selSearchProduct_id.SelectedValue & "%")
End If
I need to change this code so I can check for MULTIPLE selected items from a ListBox control (simple select tag: <select id="selSearchProduct" multiple="multiple">), however, I'm having difficulty looping through the collection to create the statement:
If (Me.selSearchApplicationType.SelectedValue.Length > 0) Then
If (sWhere.Length > 0) Then sWhere &= " AND "
sWhere &= "application like @application"
Me.sqlGridSource.SelectParameters.Add("application", "%" & Me.selSearchApplicationType.SelectedValue & "%")
End If
If (Me.selSearchProduct.SelectedValue.Length > 0) Then
If (sWhere.Length > 0) Then sWhere &= " AND "
sWhere &= "("
For Each ProductSelected in Me.selSearchProduct.Items
sWhere &= "product_id = @product" & ProductSelected.Index
If (ProductSelected.Index < Me.selSearchProduct.Items.Count) Then
sWhere &= " OR "
End If
Me.sqlGridSource.SelectParameters.Add("product" & ProductSelected.Index, CInt(ProductSelected.Value))
Next
sWhere &= ")"
End If
It's coming up with no results. I want the above script to append the WHERE statement in an way that will look like this:
sWhere &= " AND (product_id = @product1 OR product_id = @product2 OR product_id = @product3)"
so that the end result will be:
sWhere &= " AND (product_id = 13 OR product_id = 52 OR product_id = 20)"
I can't see anything wrong with the loop above that would be causing it not to get results. I know the data is there, because I can get results by creating the statement manually.
NOTE: The reason I left "If (Me.selSearchProduct.Selec
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Pushing the stuff into an array shouldn't be necessary, but I'm glad you found a solution. I'd give you better code, but I'm not near my VS2010 system right now.
ASKER
Well the reason I had to use the array was so that I didn't end up with the extra "OR" on the end. I had to keep a separate count of the selected items. Nevertheless, your post gave me the answer I needed. I didnt realize I needed to differentiate between selected and non selected after the submission. Thanks a bunch.
No qualms, but I believe you intended to assign points,but have closed the question out without doing so. If you meant to, please do so or, if necessary, flag it so an admin can get involved.
Thanks!
Thanks!
ASKER
I assigned points, but I had to attach my comment as well as part of the solution (0 points), and it just takes time for them to close it in that case. Points will be given on 3/22.
ASKER
See my code below.
ASKER
Is there an easy way to determine how many iterations have gone by without creating a ItemIndex+=1 statement?