C# asp.net网格视图批量更新所有单元格
我正在使用asp.net网格视图从我的sql数据表加载数据。我能够成功加载数据 Sql表设计: 3个克隆:位置、名字、姓氏 位置是主键 设计: Aspxpage有一个gridview,其底部有两个按钮:C# asp.net网格视图批量更新所有单元格,c#,asp.net,sql,C#,Asp.net,Sql,我正在使用asp.net网格视图从我的sql数据表加载数据。我能够成功加载数据 Sql表设计: 3个克隆:位置、名字、姓氏 位置是主键 设计: Aspxpage有一个gridview,其底部有两个按钮: 编辑 拯救 当用户点击编辑按钮时,gridview中的所有单元格都可编辑,以便用户可以编辑和保存值 我的问题出现在“保存”按钮上,无法将编辑的数据保存回SQL 以下是“保存”按钮单击的代码: protected void btnSave_Click(object sender, EventArg
protected void btnSave_Click(object sender, EventArgs e)
{
int RowIndex=0;
GridViewRow row = (GridViewRow)gvres.Rows[RowIndex];
TextBox txtLanguage1 = row.FindControl("txtFName") as TextBox;
TextBox txtLanguage2 = row.FindControl("txtLName") as TextBox;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("UPDATE UsersTable SET FirstName = @FirstName, LastName = @LastName WHERE Location = @Location", myConnection);
cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());
myConnection.Open();
cmd.ExecuteNonQuery();
gvusers.EditIndex = -1;
DataBind();
}
异常:必须声明标量变量“@Location”。您需要向名为“@Location”的SqlCommand对象添加一个参数。您提到Location是网格中的一列——您可以从该列中读取值(类似于获取名字和姓氏值的方式),也可以指定“Location”作为数据键,并从网格的DataKeys属性中获取它
我会看看Codeplex上的项目。它允许批量编辑,并且足够智能,只更新已更改的行。您需要向名为“@Location”的SqlCommand对象添加一个参数。您提到Location是网格中的一列——您可以从该列中读取值(类似于获取名字和姓氏值的方式),也可以指定“Location”作为数据键,并从网格的DataKeys属性中获取它
//@Location means that the Insert / Update expects that field to be passed in / added //to your cmd.Parameters where are you adding @Location...?
我会看看Codeplex上的项目。它允许批量编辑,并且足够智能,只更新已更改的行
//@Location means that the Insert / Update expects that field to be passed in / added //to your cmd.Parameters where are you adding @Location...?
//请看这行代码,其中where Location=@Location”,myConnection);
//添加declare cmd.Parameters.AddWithValue(“@Location”,someLocationValue.Trim())
//请看这行代码,其中where Location=@Location”,myConnection);
//添加declare cmd.Parameters.AddWithValue(“@Location”,someLocationValue.Trim())
cmd.Parameters.AddWithValue(“@Location”,txtLocation.Text.Trim())
}
cmd.Parameters.AddWithValue(“@Location”,txtLocation.Text.Trim())
}@Location意味着插入/更新需要将该字段传入/添加到cmd参数中。您在哪里添加@Location。。。?请看这行代码,其中的where Location=@Location”,myConnection);add/declare cmd.Parameters.AddWithValue(“@Location”,someLocationValue.Trim());RowIndex变量硬编码为0,因此只更新第一行。如果整个网格只有一个保存按钮,这将是一个问题。是的,这就是我现在发现的。它只更新第一行。那么我如何循环所有行并更新。@位置意味着插入/更新希望该字段传入/添加d到cmd.Parameters中,您在哪里添加@Location…?请看一行,其中有where Location=@Location“,myConnection);添加/声明cmd.Parameters.AddWithValue(“@Location”,someLocationValue.Trim());RowIndex变量硬编码为0,因此仅更新第一行。如果整个网格只有一个保存按钮,那将是一个问题。是的,这就是我现在发现的。它只更新第一行。那么我如何循环所有行并更新。
myConnection.Open();
cmd.ExecuteNonQuery();
gvusers.EditIndex = -1;
DataBind();