【发布时间】: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