0

I'm hoping to retrieve data-rows from a database using a query with a checkboxlist as a control parameter. The problem is multiple selected values on the checkboxlist only return one value as seen here...

Illustration

<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" DataSourceID="SqlDataSource1" DataTextField="Fruit" DataValueField="Fruit" ></asp:CheckBoxList>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:f-StopConnectionString %>' SelectCommand="SELECT [Fruit] FROM [Table_1]"></asp:SqlDataSource>

<br />

<asp:Button ID="Button1" runat="server" Text="Select" OnClick="Button1_Click" />

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"></asp:BoundField>
        <asp:BoundField DataField="Fruit" HeaderText="Fruit" SortExpression="Fruit"></asp:BoundField>
    </Columns>
</asp:GridView>

<asp:SqlDataSource runat="server" ID="SqlDataSource4" ConnectionString='<%$ ConnectionStrings:f-StopConnectionString %>' 
    SelectCommand="SELECT * FROM [Table_1] WHERE ([Fruit] LIKE '%' + @Fruit + '%')">
    <SelectParameters>
        <asp:ControlParameter ControlID="CheckBoxList1" PropertyName="SelectedValue" Name="Fruit" Type="String"></asp:ControlParameter>
    </SelectParameters>
</asp:SqlDataSource>

Is there a way to pass multiple selections so that the query would read:

(WHERE [Fruit] LIKE '%'Apples'%') OR (WHERE [Fruit] LIKE '%'Oranges'%')

Any help at all would be greatly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2078938
  • 947
  • 1
  • 9
  • 22

1 Answers1

0

As far as I understand, as you have provided no code, you want to use linq (or entity framework) to query database.

You need then to build lambda expression depending on user selection and pass it to your query.

See Lambda expressions trees here for start: http://www.codeproject.com/Articles/17575/Lambda-Expressions-and-Expression-Trees-An-Introdu

This can help also: Can I use custom delegate method in Where method of Entity Framework?

If you want just to pass sql select command string, then you should build custom where clause depending on user selection.

Community
  • 1
  • 1
Ivan Ičin
  • 9,672
  • 5
  • 36
  • 57