title: Insert multiple checkboxlist values to sql db asp.net C#
body: <p>I am trying to insert multiple CheckBoxList values that are INT to a SQL DB using ASP.NET C#. I have set a breakpoint in my code and the loop works but once it picks up the first selected value, it stays with that value and causes a Primary Key Constraint. In the end, it just inserts one value. After a couple days of research, all my findings show how to insert multiple strings to a SQL DB but none for INT. Any help is much appreciated.</p>
  
  <pre><code>    for (int i = 0; i &lt; cbAvailableEntities.Items.Count - 1; i++)
  {
      SqlConnection con = new SqlConnection(connString);
          SqlCommand cmd = new SqlCommand("InsertProjectEntity", con);
  
          using(con)
          {
              con.Open();
              using(cmd)
              {
                   if (cbAvailableEntities.Items[i].Selected)
                   {
                       //This code gets the Project ID that is to be associated with the new info
                       DALSectionAccessData LastProjectID = new DALSectionAccessData(connString);
                       //This puts the Project ID needed into the variable _pID
                       int _pID = LastProjectID.GetLastLogReportID();
                       cmd.CommandType = CommandType.StoredProcedure;
                       nr.Entities = cbAvailableEntities.SelectedValue;
                       cmd.Parameters.AddWithValue("@ProjectID", _pID);
                       cmd.Parameters.AddWithValue("@CorpID", nr.Entities);
                       cmd.ExecuteNonQuery();
                    }
              }
          }
  }
  </code></pre>
  
  <p>This is what the error says</p>
  
  <blockquote>
    <p>Server Error in '/' Application.</p>
    
    <p>Violation of PRIMARY KEY constraint 'PK_ProjectEntity'. Cannot
    insert duplicate key in object 'dbo.ProjectEntity'. The statement
    has been terminated.</p>
    
    <p>Description: An unhandled exception occurred during the execution of
    the current web request. Please review the stack trace for more
    information about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.Data.SqlClient.SqlException: Violation of
    PRIMARY KEY constraint 'PK_ProjectEntity'. Cannot insert duplicate
    key in object 'dbo.ProjectEntity'. The statement has been
    terminated.</p>
  </blockquote>