Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
            RepeatDirection="Horizontal">
            <asp:ListItem>A</asp:ListItem>
            <asp:ListItem>B</asp:ListItem>
            <asp:ListItem>C</asp:ListItem>
            <asp:ListItem>D</asp:ListItem>
        </asp:CheckBoxList>
        <asp:CheckBoxList ID="CheckBoxList2" runat="server"
            RepeatDirection="Horizontal">
            <asp:ListItem>F</asp:ListItem>
            <asp:ListItem>G</asp:ListItem>
            <asp:ListItem>H</asp:ListItem>
            <asp:ListItem>I</asp:ListItem>
        </asp:CheckBoxList>

I want to insert the checkboxlist values using C# in database how can i acheive so

as i have the following code for single checkboxlist1 how can i do for the multiple checkboxlist1 and checkboxlist2 , My code is as

C#
string con = ConfigurationManager.ConnectionStrings["check"].ConnectionString;
        var sSkill = "";
     
        CheckBoxList project = FindControl("CheckBoxList1") as CheckBoxList;
        foreach (ListItem oItem in project.Items)
        {
            if (oItem.Selected)
            {
                if (sSkill.Length == 0)
                    sSkill = oItem.Value;
                else
                    sSkill = sSkill + " " + oItem.Value;
            }
        }
        if (sSkill != "")
        {
            SqlConnection sqlcon = new SqlConnection(con);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [tablename] (field1 , field2)values(@skill,@text1) , (@skill,@text2 )";
            cmd.Parameters.AddWithValue("@skill", sSkill);
            cmd.Parameters.AddWithValue("@text1", TextBox1.Text);
            cmd.Parameters.AddWithValue("@text2", TextBox2.Text);
            sqlcon.Open();
            cmd.ExecuteNonQuery();
            Label1.Text = sSkill + " was saved sucessesfully";
            sqlcon.Close();
        }
Posted
v2
Comments
Can't you do like below as you did for other?
CheckBoxList project = FindControl("CheckBoxList2") as CheckBoxList;
.......
.....
....
ManeeshaPP 28-Oct-13 11:19am    
HI TADIT
USING YOUR CODE I HAVE INSERTED DATA TO DB.Its working fine .thanks a lot as i was searching for solution.pLS CAN U TELL ME NOW HOW TO SHOW THIS DATA AS SELECTED WHEN NEXT TIME PAGE IS LOADED

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900