mardi 4 août 2015

Saving multiple selected checkboxlist into database asp.net

i'm working with ASP.NET web application and i'm having a problem with adding multiple selected checkboxlist into the database . i want the user to be able to chose more then one Check Box. i have tried some method but it didn't work the database still empty even my database connection work fine with others tables .

Destinations table :

CREATE TABLE [dbo].[Destinations]
(
    [Id] INT NOT NULL PRIMARY KEY, 
    [North] INT NULL DEFAULT 0, 
    [West] INT NULL DEFAULT 0, 
    [South] INT NULL DEFAULT 0, 
    [East] INT NULL DEFAULT 0, 
    CONSTRAINT [FK_Destinations_DeliveryMen] FOREIGN KEY ([Id]) REFERENCES [DeliveryMen]([Delivery_ID])
)

and my CheckBoxList is :

   <asp:CheckBoxList ID="CheckBoxList2" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" Width="263px">
                <asp:ListItem text="North " Value="1"></asp:ListItem>
                <asp:ListItem text="South " Value="2"></asp:ListItem>
                <asp:ListItem text="West " Value="3"></asp:ListItem>
                <asp:ListItem text="East " Value="4"></asp:ListItem>
            </asp:CheckBoxList>

UPDATE : the C# code that i have tried :

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString);
conn.Open();
string insertcheckboxlist = "insert into Destinations (North,West,South,East) values(@1,@3,@2,@4))";
SqlCommand comm = new SqlCommand(insertcheckboxlist, conn);
comm.Parameters.AddWithValue("@CheckBoxList2", CheckBoxList2.SelectedItem.Value);

Aucun commentaire:

Enregistrer un commentaire