How to insert multiple checkboxes into an sql database

Hi
I want to insert multiple checkboxes into an sql table using asp.net. At the mment it only inserts the first value checked, so not sure if i am doing it the right way.

Part of the SQL code-----------------------------------------
strInsert = “Insert subscriptions(subscription) Values (@subscription)”
cmdInsert.Parameters.Add( “@subscription”, check.SelectedValue)

Asp.net code -----------------------------------------------
<td> Select Offers:</td>
<td><asp:CheckBoxList id=“check”
runat=“server”>
<asp:ListItem Value=“A” />
<asp:ListItem Value=“B” />
<asp:ListItem Value=“C” />
<asp:ListItem Value=“D” />
</asp:CheckBoxList>


On the database it only insert the first value checked and ignores the rest

Any idea how to fix this problem?

thanks in advance

I’d personally list each option as it’s own check box control and then apply the parameter in the SQL statement:

<asp:CheckBox id="chk1" runat="server" Checked='<%#Bind("ColumnName")%>'/>

<asp:CheckBox id="chk1" runat="server" Checked='<%#Bind("ColumnName2")%>'/>
INSERT INTO Table(ColumnName, ColumnName2)VALUES(@ColumnName, @ColumnName2)

And then add it to your parameters collection of the command object or to the SQL data source.

Thanks but it didnt like it. it shows the following error message:

‘SelectedValue’ is not a member of ‘System.Web.UI.WebControls.CheckBox’.

strInsert = "Insert subscriptions(subscription) Values (@ColumnName) "

cmdInsert.Parameters.Add( “@ColumnName”, chk1.SelectedValue)

<asp:CheckBox id=“chk1” runat=“server” Checked=‘<%#Bind(“ColumnName”)%>’/>

Obviously you took me a little more literal than I expected you to. When I said add each item to one check box I meant this:

<asp:CheckBox id="check" runat="server" Checked=<'&#37;#Bind("Column")%>'/>

Replace Column with the name of your column field in your database table. Then add additional ones for each check box entry you need. I don’t think SelectedValue will work in this scenario. Then add your column(s) to the parameters collection of the command object.

Ok. The column name on the table is called subscription so this is what I did

strInsert = "Insert subscriptions(subscription) Values (@subscription) "

<asp:CheckBox id=“check” runat=“server” Checked=<‘%#Bind(“subscription”)%>’/>

but I am not sure what to put on the parameters the parameters for the check box. is this right?

cmdInsert.Parameters.Add( “@subscription”, check.Checked)

It should be right…did you try it? If so, if it compiles and inserts properly, then it works. I usually use SqlDataSources in this environment instead of the code behind.

Like:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"  ConnectionString="<%$ ConnectionStrings:SOConnectionString %>" InsertCommand="INSERT INTO TABLE (subscription)VALUES(@subscription)>
       <InsertParameters>
       <asp:Parameter Name="subscription" Type="String"/>
</InsertParameters>
     </asp:SqlDataSource>

Here you go:

http://www.codersbarn.com/post/2008/10/12/Bind-CheckBoxList-to-DataSet.aspx

Anthony :slight_smile: