title: how to save multiple selected checkboxlist to database asp .net c#
body: <p>I have two checkboxlists CandidateName and Payment,  and a dropdownlist. User can do multiple selections in checkboxlists and one selection at dropdownlist. I am populating those data of chechboxlists and dropdownlist to Panel nested inside UpdatePanel where gridview exist.. When I click on Add Button to open up Panel, all data is properly populating. When I tried to save multiple selected checkedboxlists through "for loop" at debugging, it is not going inside the loop to accumulate checked data with comma to save at database. Instead, after chekcing  "if (cblPayment1.Items[j].Selected == true)", it skips over to another query statement. Please help me advise. </p>
  
  <pre><code> protected void btnAddNew_Click(object sender, EventArgs e)
  {
      try
      {
  
          string strcbl1 = string.Empty;
          string strcbl2 = string.Empty;
          compCon = new SqlConnection(strConnectionString);
          compCon.Open();
  
  
          for (int i = 0; i &lt; cblCandidateName1.Items.Count-1; i++)
          {
              if (cblCandidateName1.Items[i].Selected ==true)
              {
                  strcbl1 = strcbl1 + cblCandidateName1.Items[i].Value.ToString() + ",";
              }
          }
  
          for (int j = 0; j &lt; cblPayment1.Items.Count - 1; j++)
          {
              if (cblPayment1.Items[j].Selected == true)
              {
                  strcbl2 += cblPayment1.Items[j].Value.ToString() + ",";
              }
          }
  
  
         string InsertSchedule = "INSERT INTO ScheduleEmail(CANDIDATE_ID, PAYMENT_ID,     TEMPLATE_ID)VALUES(@strCID, @strPID, @strCourseID)";
          SqlCommand InsertScheduleCommand = new SqlCommand(InsertSchedule, compCon);
          InsertScheduleCommand.Connection = compCon;
  
  
          InsertScheduleCommand.Parameters.AddWithValue(@"strCID", strcbl1);
          InsertScheduleCommand.Parameters.AddWithValue(@"strPID", strcbl2);
          InsertScheduleCommand.Parameters.AddWithValue(@"strCourseID", strCourseID);
  
          InsertScheduleCommand.ExecuteNonQuery();
          compCon.Close();
      }
      catch (Exception ex)
      {
          ShowPopUpMsg("Data is failed to save in table. Please contact your system administrator \n" + ex.ToString());
      }
      finally
      { 
      DispalyGridViewScheduleEmail();
  
      compCon.Close();
      }
  
  }
  </code></pre>
  
  <p>THis is my HTML Code.</p>
  
  <pre><code>&lt;!--Panel to add new record--&gt;&lt;asp:Panel ID="panelAddNew" runat="server" 
  style="display:none; background-color:gray;"        ForeColor="Black" Width="500" Height="550"&gt;
  &lt;asp:Panel ID="panelAddNewTitle" runat="server" 
   style="cursor:move;font-family:Tahoma;                       
   padding:2px;" HorizontalAlign="Center" BackColor="Blue" ForeColor="White" Height="25"&gt;
  &lt;b&gt;Add New&lt;/b&gt;&lt;/asp:Panel&gt;
   &lt;table width="100%" style="padding:5px"&gt;
              &lt;tr&gt;
              &lt;td colspan="3"&gt;
                  &lt;asp:Label ID="lblStatus1" runat="server"&gt;&lt;/asp:Label&gt;
              &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;b&gt;Select Candidate Name(s)&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;b&gt;:&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;asp:CheckBoxList ID="cblCandidateName1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="table"&gt;
                  &lt;/asp:CheckBoxList&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;b&gt;Select Payments&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;b&gt;:&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt;
                     &lt;asp:CheckBoxList ID="cblPayment1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table"&gt;&lt;/asp:CheckBoxList&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
  
  
              &lt;tr&gt;
                  &lt;td&gt;&lt;b&gt;Course Name&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;b&gt;:&lt;/b&gt;&lt;/td&gt;
                  &lt;td&gt; &lt;asp:DropDownList ID="ddlCourseName" runat="server"&gt;      &lt;/asp:DropDownList&gt;            
  
  
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;br /&gt;
              &lt;div align="center"&gt;
              &lt;asp:Button ID="btnAddNew2" runat="server" Width="70" Text="Add" OnClick="btnAddNew_Click"  ValidationGroup="add"/&gt;
              &amp;nbsp;
  
              &lt;asp:Button ID="btnCancel1" runat="server" Width="70" Text="Cancel" CausesValidation="false" OnClick="Cancel_Click" ValidationGroup="add"/&gt;
          &lt;/div&gt;
      &lt;/asp:Panel&gt;   
          &lt;!--End of Panel to add new record--&gt;
  </code></pre>