简体   繁体   English

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

[英]save selected checkbox in gridview row c# asp

i want to save each row which checked using sqldatasource, but i got error The variable name '@Approved_By' has already been declared.我想保存使用 sqldatasource 检查的每一行,但出现错误变量名称 '@Approved_By' 已经声明。 Variable names must be unique within a query batch or stored procedure.变量名在查询批处理或存储过程中必须是唯一的。

am i wrong in looping?我循环错了吗?

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++;
}

The variable name has already been declared message is pretty obvious: you're adding same parameter names in foreach loop multiple times for every iteration.变量名称已经被声明消息非常明显:您在foreach循环中为每次迭代多次添加相同的参数名称。

Add SqlDataSource3.UpdateParameters.Clear();添加SqlDataSource3.UpdateParameters.Clear(); method either before adding UpdateParameters or after executing Update() method to clear parameter collection before next iteration starts:在添加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++;
}
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());
            }
        }  
    }
}

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

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