在gridview行c#asp中保存选中的复选框

时间:2017-11-16 04:13:58

标签: c# asp.net sql-server sql-server-2008 gridview

我想保存使用sqldatasource检查的每一行,但我收到错误变量名称' @ Approved_By'已经宣布。变量名在查询批处理或存储过程中必须是唯一的。

我在循环中错了吗?

int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
        bool chk = chkRow.Checked;

        if (chk = chkRow.Checked)
        {
            SqlDataSource3.UpdateParameters.Add("Approved_By", Session["username"].ToString());
            SqlDataSource3.UpdateParameters.Add("Kode_Personel", GridView1.Rows[row.RowIndex].Cells[0].Text);
            SqlDataSource3.Update();
        }
    }
    i++;
}

2 个答案:

答案 0 :(得分:2)

变量名已经被声明消息非常明显:您每次迭代都会在foreach循环中多次添加相同的参数名称。

在添加SqlDataSource3.UpdateParameters.Clear();之前或在执行UpdateParameters方法之后添加Update()方法以在下一次迭代开始之前清除参数集合:

int i = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
        bool chk = chkRow.Checked;

        if (chk = chkRow.Checked)
        {
            // add this line so that UpdateParameter collection cleared before adding new parameters
            SqlDataSource3.UpdateParameters.Clear();

            SqlDataSource3.UpdateParameters.Add("Approved_By", Session["username"].ToString());
            SqlDataSource3.UpdateParameters.Add("Kode_Personel", GridView1.Rows[row.RowIndex].Cells[0].Text);

            SqlDataSource3.Update();

            // or you can clear UpdateParameters collection here
        }

    }
    i++;
}

答案 1 :(得分:0)

protected void btn_insert_Click(object sender, EventArgs e)
        {
           
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    select++;
                }
            }

            if (select == 0)
            {
                Page.RegisterStartupScript("Alert Message", "<script language='javascript'>alert('Please check one checkbox records');</script>");
                return;
            }

            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
            {
                string sid = GridView1.Rows[i].Cells[1].Text;
                string sname = GridView1.Rows[i].Cells[2].Text;
                string smarks = GridView1.Rows[i].Cells[3].Text;
                string saddress = GridView1.Rows[i].Cells[4].Text;
                GridViewRow row = GridView1.Rows[i];
                CheckBox Chbox = (CheckBox)row.FindControl("chb1");
                if (Chbox.Checked == true)
                {
                    InsertData(sid, sname, smarks, saddress);
                }
            }
            Response.Write("Record inserted successfully");
            }

        void InsertData(String sid, String sname, String smarks,string saddress)
        {
            SqlConnection con = new SqlConnection(connStr);
            try
            {
                con.Open();
                com = new SqlCommand("insert into student values('" + sid + "','" + sname + "','" + smarks + "','" + saddress + "')", con);
                com.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }  
    }
}