我正在使用带分页的网格视图。我想只在数据库中插入选中的复选框值。多个值正在选择但值没有插入数据库。我已经尝试了下面的代码。帮助我从这些问题
ASPX代码
<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>
代码文件
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();
}
}
}
}
答案 0 :(得分:0)
您在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);