Click here to Skip to main content
15,915,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
 protected void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection conn = null;
        String str = "";
        try
        {
            for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
            {

                if (CheckBoxList1.Items.Selected)
                {

                    if (str == "")
                        str = CheckBoxList1.Items.Text;
                    else
                        str += "," + CheckBoxList1.Items.Text;

                }
            }



            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["PConnStr"].ConnectionString);
            conn.Open();
            string sql = "insert into Doctor(D_Name,Qualification,Specialisation,Email_id,AvailableDay,AvailableTime,Sex)Values(@fname,@qualification,@specialisation,@email,"','"+str+ "','",@availabletime,@sex)";
            SqlCommand cmd = new SqlCommand(sql, conn);

.
.
.
}
Posted
Updated 8-Apr-12 8:10am
v2

1 solution

Hi ,
Try this will Guide you .

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(@"Data Source=MOHAMED-PC\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True");
       con.Open();
       foreach (ListItem item in CheckBoxList1.Items)
       {
          string sql =string.Format("insert into Table_1 (chk) values ('{0}')",item.Selected);
           SqlCommand cmd = new SqlCommand(sql, con);
           cmd.CommandType = System.Data.CommandType.Text;
           cmd.ExecuteNonQuery();
       }

   }

ASP.NET
<div>
      <asp:CheckBoxList ID="CheckBoxList1" runat="server">
          <asp:ListItem>1</asp:ListItem>
          <asp:ListItem>2</asp:ListItem>
          <asp:ListItem Selected="True">3</asp:ListItem>
          <asp:ListItem>4</asp:ListItem>
          <asp:ListItem>5</asp:ListItem>
      </asp:CheckBoxList>
      <br />
      <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  </div>

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
lara shaikh 9-Apr-12 13:21pm    
Thanks in advance.
i wanted to enter all details on click of a button.
What is (chk)in insert statement.what to be written there.
when i run this code it is throwing an error..

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