【问题标题】:save selected checkbox in gridview row c# asp在gridview行中保存选中的复选框c#asp
【发布时间】:2018-04-29 12:14:03
【问题描述】:

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

我循环错了吗?

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

【问题讨论】:

  • 认为变量@Approved_By 声明了不止一次,请检查一下
  • 是的,我需要使用变量@Approved_By 更新每一个检查过的行。所以我使用foreach?如果只有一个检查没问题,但如果 2 个或更多检查得到错误消息..我在 foreach 循环中错了吗?
  • 在foreach循环中添加UpdateParameters之前执行SqlDataSource3.UpdateParameters.Clear()怎么样?这行得通吗?
  • 非常感谢 Tetsuya,它的工作

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


【解决方案1】:
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());
            }
        }  
    }
}

【讨论】:

    【解决方案2】:

    变量名已经被声明消息很明显:你在foreach循环中为每次迭代添加了多次相同的参数名。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2021-12-31
      相关资源
      最近更新 更多