简体   繁体   English

将多个复选框列表值插入sql db asp.net C#

[英]Insert multiple checkboxlist values to sql db asp.net C#

I am trying to insert multiple CheckBoxList values that are INT to a SQL DB using ASP.NET C#. 我正在尝试使用ASP.NET C#将多个INT的CheckBoxList值插入到SQL DB中。 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. 经过几天的研究,我的所有发现都显示了如何将多个字符串插入SQL DB,而对于INT则没有。 Any help is much appreciated. 任何帮助深表感谢。

    for (int i = 0; i < 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();
                  }
            }
        }
}

This is what the error says 这就是错误的意思

Server Error in '/' Application. “ /”应用程序中的服务器错误。

Violation of PRIMARY KEY constraint 'PK_ProjectEntity'. 违反PRIMARY KEY约束“ PK_ProjectEntity”。 Cannot insert duplicate key in object 'dbo.ProjectEntity'. 无法在对象“ dbo.ProjectEntity”中插入重复键。 The statement has been terminated. 该语句已终止。

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_ProjectEntity'. 异常详细信息:System.Data.SqlClient.SqlException:违反PRIMARY KEY约束'PK_ProjectEntity'。 Cannot insert duplicate key in object 'dbo.ProjectEntity'. 无法在对象“ dbo.ProjectEntity”中插入重复键。 The statement has been terminated. 该语句已终止。

Inside your loop, you are checking in the if() statement to see if Item[i] is selected, but if it is you are not doing anything with Item[i] to differentiate one iteration from the next. 在循环内部,您正在检查if()语句以查看是否选择了Item[i] ,但如果是,则不对Item[i]进行任何操作来区分一次迭代和下一次迭代。 Therefore you are running the same exact code for every selected item in the checkboxlist, rather than doing something different for each. 因此,您要为复选框列表中的每个选定项目运行相同的确切代码,而不是为每个项目执行不同的操作。

Try changing this: 尝试更改此:

cmd.Parameters.AddWithValue("@CorpID", nr.Entities);

to something like this: 像这样:

cmd.Parameters.AddWithValue("@CorpID", cbAvailableEntities.Items[i].Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM