当gridview中给出分页时,值没有插入

本文关键字:插入 分页 gridview | 更新日期: 2023-09-27 18:05:20

我正在使用带有分页的网格视图。我想在数据库中只插入选中的复选框值。多个值正在选择,但值没有插入数据库。我已经尝试了下面的代码。帮我解决这些问题

ASPX CODE

<asp:GridView ID="gvsubject" runat="server" AutoGenerateColumns = "false" CssClass="mGrid1"  AllowPaging ="true" OnPageIndexChanging="gvsubject_PageIndexChanging" OnRowDataBound="gvsubject_RowDataBound">
        <Columns>
            <asp:BoundField DataField="subject" HeaderText="subject" SortExpression="subject"/>
            <asp:TemplateField HeaderText="Select">
            <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" />
             </HeaderTemplate> 
                    <EditItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" onclick = "checkAll(this);"/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" onclick = "Check_Click(this)"/>
                    </ItemTemplate>
                </asp:TemplateField>
        </Columns>
    </asp:GridView>

CODE File

         ArrayList CheckBoxArray;
         if (ViewState["CheckBoxArray"] != null)
         {
             CheckBoxArray = (ArrayList)ViewState["CheckBoxArray"];
         }
         else
         {
             CheckBoxArray = new ArrayList();
         }
         if (IsPostBack)
         {
             int CheckBoxIndex;
             bool CheckAllWasChecked = false;
             CheckBox chkAll = (CheckBox)gvsubject.HeaderRow.Cells[0].FindControl("chkAll");
             string checkAllIndex = "chkAll-" + gvsubject.PageIndex;
             if (chkAll.Checked)
             {
                 if (CheckBoxArray.IndexOf(checkAllIndex) == -1)
                 {
                     CheckBoxArray.Add(checkAllIndex);
                 }
             }
             else
             {
                 if (CheckBoxArray.IndexOf(checkAllIndex) != -1)
                 {
                     CheckBoxArray.Remove(checkAllIndex);
                     CheckAllWasChecked = true;
                 }
             }
             for (int i = 0; i < gvsubject.Rows.Count; i++)
             {
                 if (gvsubject.Rows[i].RowType == DataControlRowType.DataRow)
                 {
                     CheckBox chk = (CheckBox)gvsubject.Rows[i].Cells[0].FindControl("CheckBox1");
                     CheckBoxIndex = gvsubject.PageSize * gvsubject.PageIndex + (i + 1);
                     if (chk.Checked)
                     {
                         if (CheckBoxArray.IndexOf(CheckBoxIndex) == -1 && !CheckAllWasChecked)
                         {
                             CheckBoxArray.Add(CheckBoxIndex);
                         }
                     }
                     else
                     {
                         if (CheckBoxArray.IndexOf(CheckBoxIndex) != -1 || CheckAllWasChecked)
                         {
                             CheckBoxArray.Remove(CheckBoxIndex);
                         }
                     }
                 }
             }
         }
         ViewState["CheckBoxArray"] = CheckBoxArray;
         gvsubject.DataSource = dt;
         gvsubject.DataBind();
    }
protected void btnno_Click(object sender, EventArgs e)
    {
        string semester = ddlsemester.SelectedValue;
        string section = txtsection.Text;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
       foreach (GridViewRow gv in gvsubject.Rows)
        {
            if (gv.RowType == DataControlRowType.DataRow)
            {
                CheckBox chk = (gv.Cells[0].FindControl("CheckBox1") as CheckBox);
                if (chk.Checked)
                {
                    SqlCommand cmd = new SqlCommand("insert into tblassignsub(semester,section,subject,academicyear) values (@semester,@section,@subject,@academicyear)");
                    con.Open();
                    cmd.Parameters.AddWithValue("@semester", ddlsemester.SelectedValue);
                    cmd.Parameters.AddWithValue("@section", txtsection.Text);
                    cmd.Parameters.AddWithValue("@subject", gv.Cells[0].Text);
                    cmd.Parameters.AddWithValue("@academicyear", txtacdyear.Text);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }

当gridview中给出分页时,值没有插入

您在SqlCommand中丢失了一个连接字符串

SqlCommand cmd = new SqlCommand("insert into tblassignsub(semester,section,subject,academicyear) values (@semester,@section,@subject,@academicyear)");

试试这个

SqlCommand cmd = new SqlCommand("insert into tblassignsub(semester,section,subject,academicyear) values (@semester,@section,@subject,@academicyear)",con);