title: C# - SQL Insert Value and ID of checkbox From CheckboxList
body: <p><strong>The Issue:</strong> </p>
  
  <p>I populate a checkbox list from a SQL table:</p>
  
  <pre><code>public static List&lt;string&gt; populateCheckBoxes(string type)
          {
              List&lt;string&gt; items = new List&lt;string&gt;();
              items.Add("");
  
              SqlConnection conn = new SqlConnection(connectionString);
              SqlCommand sqlcmd;
              switch (type)
              {
                  case "referralorsignposting":
                      sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment WHERE type = 'Referral or Signposting' ORDER BY order_no, name", conn);
                      break;
                  case "actionstaken":
                      sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment WHERE type = 'Actions Taken' ORDER BY order_no, name", conn);
                      break;
                  default:
                      sqlcmd = new SqlCommand("SELECT * FROM SWApp_List_Equipment", conn);
                      break;
              }
              SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
              DataTable dt = new DataTable();
              da.Fill(dt);
              foreach (DataRow dr in dt.Rows)
              {
                  items.Add(dr["name"].ToString());
                  CheckboxIDRecord = dr["id"].ToString();
                  //items.Add(dr["VisitTime"] + " " + dr["PropPostcode"]);
              }
  
              return items;
          }
  </code></pre>
  
  <p>I have iterated through each value that is selected in the "checkboxlist" and this  inserts each value selected: </p>
  
  <pre><code>foreach (var item in saw.actionsTakenCheckBoxList)
                  {   //ADD ACTIONS AND REFERRAL 
                      SqlCommand add = new SqlCommand("INSERT INTO SWApp_CheckboxAnswers (SW_ID, Checkbox_ID, Checkbox_Section, Checkbox_Type, Checkbox_Answer) VALUES(@SW_ID,@Checkbox_ID,@Checkbox_Section,@Checkbox_Type,@Checkbox_Answer) ");
                      add.CommandType = CommandType.Text;
                      add.Connection = sqlcon;
                      add.Parameters.AddWithValue("@SW_ID", "");
                      add.Parameters.AddWithValue("@Checkbox_ID", "");
                      add.Parameters.AddWithValue("@Checkbox_Section", "");
                      add.Parameters.AddWithValue("@Checkbox_Type", "");
                      add.Parameters.AddWithValue("@Checkbox_Answer", "");
                      add.Parameters["@SW_ID"].Value = saw.EntryID.ToString();
                      add.Parameters["@Checkbox_ID"].Value = CheckboxIDRecord.ToString();
                      add.Parameters["@Checkbox_Section"].Value = "SmokeDetectionReferral";
                      add.Parameters["@Checkbox_Type"].Value = "";
                      add.Parameters["@Checkbox_Answer"].Value = item.ToString();
                      sqlcon.Open();
                      add.ExecuteNonQuery();
                      sqlcon.Close();
                  }
  </code></pre>
  
  <p>As you can see what i have currently tried only inputs the ID for the first value selected in the Checkboxlist. </p>
  
  <p><strong>The Aim</strong>: </p>
  
  <p>The aim is to have insert the value of the checkbox which is "name" and also the "id" of the each item. </p>
  
  <p><strong>Research:</strong></p>
  
  <p>I tried following this article to put the items into an array but ended up with a 'Array was out of bounds of the index' which led me to the second article. </p>
  
  <p><a href="https://stackoverflow.com/questions/17754667/pass-items-from-checkboxlist-to-sql-server-table">Pass items from checkboxlist to SQL Server table</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/19124619/index-was-out-of-bounds-of-array-c-sharp-forms">Index was out of bounds of array? c# forms</a></p>
  
  <p>I would appreciate any guidance with this. Thanks. </p>