c# - 已添加具有相同键的项目有关将gridvalues添加到数据库的问题

c# - 已添加具有相同键的项目有关将gridvalues添加到数据库的问题,第1张

在下面的代码中,我想将gridview值插入到数据库中。当我尝试插入值时,它会抛出错误“已经添加了具有相同键的项目。”。只有第一行被添加到数据库剩余的行是没有添加。请帮助我解决问题。

 protected void btnSave_Click(object sender, EventArgs e)
        {  string Check=string.Empty;
            string BranchID = dpbranch.SelectedValue;
            string ClassID = dpClassName.SelectedValue;
            string SectionID = dpsection.SelectedValue;
            TestSchool.SchoolBusinessLyr.SchoolBizClient Attendance = new TestSchool.SchoolBusinessLyr.SchoolBizClient();
            System.Collections.Generic.Dictionary<string, string> AssignStudentAttendance = new System.Collections.Generic.Dictionary<string, string>();
            DataSet ds = new DataSet();
            foreach (GridViewRow row in gdstudentattendance.Rows)
            {
                String cellText = row.Cells[0].Text;
                String RegisterNo = row.Cells[2].Text;
                int rowIndex = 0;
                for (int i = 0; i < gdstudentattendance.Rows.Count; i  )
                {

                    //extract the TextBox values
                    CheckBox StudentAttendance = (CheckBox)gdstudentattendance.Rows[i].Cells[i].FindControl("chkattendance");
                  if(StudentAttendance.Checked==true)
                  {
                      Check="true";
                  }
                  else
                  {
                       Check="false";
                  }
                    AssignStudentAttendance.Add("BranchID", BranchID);
                    AssignStudentAttendance.Add("ClassID", ClassID);
                    AssignStudentAttendance.Add("SectionID", SectionID);
                    AssignStudentAttendance.Add("StudentID", cellText.ToString());
                    AssignStudentAttendance.Add("RegisterNo", RegisterNo.ToString());
                    AssignStudentAttendance.Add("Attendance", Check.ToString());
                    Attendance.InsertStudentAttendance(AssignStudentAttendance);

                }
            }

        }

最佳答案:

1 个答案:

答案 0 :(得分:1)

您应该为每一行创建新的词典。像:

protected void btnSave_Click(object sender, EventArgs e)
        {  string Check=string.Empty;
            string BranchID = dpbranch.SelectedValue;
            string ClassID = dpClassName.SelectedValue;
            string SectionID = dpsection.SelectedValue;
            TestSchool.SchoolBusinessLyr.SchoolBizClient Attendance = new TestSchool.SchoolBusinessLyr.SchoolBizClient();
            System.Collections.Generic.Dictionary<string, string> AssignStudentAttendance;
            DataSet ds = new DataSet();
            foreach (GridViewRow row in gdstudentattendance.Rows)
            {
                //Create new dictionary for each row
                AssignStudentAttendance = new System.Collections.Generic.Dictionary<string, string>();

                String cellText = row.Cells[0].Text;
                String RegisterNo = row.Cells[2].Text;
                int rowIndex = 0;
                for (int i = 0; i < gdstudentattendance.Rows.Count; i  )
                {

                    //extract the TextBox values
                    CheckBox StudentAttendance = (CheckBox)gdstudentattendance.Rows[i].Cells[i].FindControl("chkattendance");
                  if(StudentAttendance.Checked==true)
                  {
                      Check="true";
                  }
                  else
                  {
                       Check="false";
                  }
                    AssignStudentAttendance.Add("BranchID", BranchID);
                    AssignStudentAttendance.Add("ClassID", ClassID);
                    AssignStudentAttendance.Add("SectionID", SectionID);
                    AssignStudentAttendance.Add("StudentID", cellText.ToString());
                    AssignStudentAttendance.Add("RegisterNo", RegisterNo.ToString());
                    AssignStudentAttendance.Add("Attendance", Check.ToString());
                    Attendance.InsertStudentAttendance(AssignStudentAttendance);

                }
            }

        }
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复