【问题标题】:Error updating fields in SQL Server front end application更新 SQL Server 前端应用程序中的字段时出错
【发布时间】:2023-03-13 04:10:01
【问题描述】:

我正在处理一个几乎完整的应用程序,除了我的一页。

页面上有一个gridview,它显示来自数据库的数据,它可以工作,问题是当用户尝试更新它抛出错误的信息时。

这是我的代码 - 谁能看到我做错了什么以及如何解决这个问题?

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];

    int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    int Period_Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    int Evo_StockLink = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);

    string Evo_ItemCode = (row.Cells[4].Controls[0] as TextBox).Text;
    string Evo_Description = (row.Cells[5].Controls[0] as TextBox).Text;

    float UnitRate = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    float MinRate = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    float RateBeforeSevenDays  = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
    float RateAfterSevenDays = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("UPDATE rates SET Period_Id = @Period_Id, Evo_StockLink = @Evo_StockLink, Evo_ItemCode = @Evo_ItemCode, Evo_Description = @Evo_Description, MinRate = @MinRate, RateBeforeSevenDays = @RateBeforeSevenDays, RateAfterSevenDays = @RateAfterSevenDays  WHERE Id = @Id"))
        {
            cmd.Parameters.AddWithValue("@Id", Id);
            cmd.Parameters.AddWithValue("@Period_Id", Period_Id);
            cmd.Parameters.AddWithValue("@Evo_StockLink", Evo_StockLink);
            cmd.Parameters.AddWithValue("@Evo_ItemCode", Evo_ItemCode);
            cmd.Parameters.AddWithValue("@Evo_Description", Evo_Description);
            cmd.Parameters.AddWithValue("@UnitRate", UnitRate);
            cmd.Parameters.AddWithValue("@MinRate", MinRate);
            cmd.Parameters.AddWithValue("@RateBeforeSevenDays", RateBeforeSevenDays);
            cmd.Parameters.AddWithValue("@RateAfterSevenDays", RateAfterSevenDays);

            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

    GridView1.EditIndex = -1;
    this.BindGrid();
}

显示的错误是:

索引超出范围。必须为非负数且小于集合的大小。
参数名称:索引

异常详细信息:System.ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。
参数名称:索引

来源错误:

第 61 行:{
第 62 行:GridViewRow 行 = GridView1.Rows[e.RowIndex];
第 63 行:int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
第 64 行:int Period_Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
第 65 行:int Evo_StockLink = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);

【问题讨论】:

  • 如果您不告诉我们错误是什么,我们如何提供帮助?
  • 另外,不要使用AddWithValue
  • 调试时,该行代码中的哪些索引超出范围?那里有两个数组引用,DataKeys[e.RowIndex]Values[0]。其中之一超出范围。调试并找出哪个。代码假设存在某些东西,但实际上不存在,您需要在尝试使用该数据之前检查数据是否存在。
  • 出于其他原因已投票结束该问题,但也与以下内容重复:stackoverflow.com/questions/20940979/…

标签: c# sql asp.net web-applications


【解决方案1】:

在 aspx 中确保您有 DataKeyNames = "Id",您还使用 Datakeys 传递所有字段,这会将整个数据库更新为该值。

【讨论】:

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