【问题标题】:asp.net grid view bulk updating all cellsasp.net 网格视图批量更新所有单元格
【发布时间】:2011-12-13 14:33:56
【问题描述】:

我正在使用 asp.net 网格视图从我的 sql 数据表中加载数据。我能够成功加载数据。

Sql 表设计: 3 列:位置、名字、姓氏

位置是主键。

设计:

Aspxpage 有一个gridview,底部有两个按钮:

  1. 编辑
  2. 保存

当用户点击编辑按钮时,gridview 中的所有单元格都可以编辑,以便用户可以编辑和保存值。

我的问题出现在保存按钮上,我无法将编辑后的数据保存回 SQL。

这是点击保存按钮的代码:

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 意味着插入/更新期望该字段被传递/添加到您的 cmd.Parameters 你在哪里添加@Location...?看看你有这个 WHERE Location = @Location", myConnection); 添加/声明 cmd.Parameters.AddWithValue("@Location", someLocationValue.Trim());
  • RowIndex 变量被硬编码为 0,因此只会更新第一行。如果整个网格只有 1 个保存按钮,那将是一个问题。
  • 是的,这就是我现在发现的。它只更新第一行..那么我如何遍历所有行并更新。

标签: c# asp.net sql


【解决方案1】:

您需要向名为“@Location”的 SqlCommand 对象添加一个参数。您提到 Location 是网格中的列之一-您可以从列中读取值(类似于您获取名字和姓氏值的方式),或者您可以将“Location”指定为数据键,并从网格的 DataKeys 属性中获取。

我会查看 Codeplex 上的 ASP.NET Real World Controls 项目。它允许批量编辑,而且它足够聪明,只更新已更改的行。

【讨论】:

    【解决方案2】:
    //@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); //添加声明 cmd.Parameters.AddWithValue("@Location", someLocationValue.Trim());

    【讨论】:

      【解决方案3】:
      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;
          TextBox txtLanguage3 = row.FindControl("txtLocation") 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());
      

      cmd.Parameters.AddWithValue("@Location", txtLocation.Text.Trim());

      myConnection.Open();
      cmd.ExecuteNonQuery();
      gvusers.EditIndex = -1;
      DataBind();
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        相关资源
        最近更新 更多