2016-06-24 8 views
-1

私の1ページを除いてほぼ完了しているアプリケーションで作業しています。SQL Serverフロントエンドアプリケーションのフィールドの更新中にエラーが発生しました

このページでは、動作するデータベースからのデータを表示しているグリッドビューです。問題は、エラーをスローしている情報をユーザーが更新しようとしたときです。

これは私のコードです - 誰かが私が間違っていることを見ることができ、どうすればこの問題を解決できますか?

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(); 
} 

示すエラーは、次のとおりです。ASPXで

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:

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

+3

エラーの内容を教えていただけない場合は、どうすればよろしいですか? – DavidG

+1

また、['AddWithValue'](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)を使用しないでください! – DavidG

+1

これをデバッグすると、そのコード行のどのインデックスが範囲外ですか?そこには、 'DataKeys [e.RowIndex]'と 'Values [0]'の2つの配列参照があります。そのうちの1つは範囲外です。デバッグして、どれを見つけるか。コードではないときに何かがあると仮定していますが、そのデータを使用する前にデータが存在するかどうかを確認する必要があります。 – David

答えて

0

あなたは=「ID」DataKeyNamesを持っていることを確認、あなたもDatakeysにこれを使用して、すべてのフィールドを渡すと、その値に全体のDBを更新します。

関連する問題