0

Why i'm getting this problem? 'ArgumentOutOfRangeException' was unhandled' in line #5.

  • An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

  • Additional information: Index was out of range. Must be non-negative and less than the size of the collection. The error points to [e.RowIndex]

      private void Sett_icts_DGV_prodAssign_CellValueChanged(object sender, DataGridViewCellEventArgs e)
             string prodid;
             int prodid1;
          {
              prodid = Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString();
    
              if (prodid == "")
              {
    
                  prodid1 = 0;
              }
              else
              {
                  prodid1 = Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignid"].Value.ToString());
              }
    
              if (prodid1 == 0)
    
              {
                  conn2.Open();
                  string query601A = "INSERT INTO ihsasprodassign(prodassigndesc, prodassignkey, prodassigndte, prodassigntme, prodassignstat) VALUES('" + Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassigndesc"].Value.ToString() + "' , '" + Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignkey"].Value.ToString()) + "' , '" + DateTime.Now + "','" + DateTime.Now + "', 'A')";
                  cmda = conn2.CreateCommand();
                  cmda.CommandType = CommandType.Text;
                  cmda.CommandText = query601A;
                  cmda.ExecuteNonQuery();
                  conn2.Close();
    
              }
              else
              {
                  conn2.Open();
                  string query601B = "UPDATE ihsasprodassign set prodassigndesc='" + Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassigndesc"].Value.ToString() + "', prodassignkey='" + Convert.ToInt32(Sett_icts_DGV_prodAssign.Rows[e.RowIndex].Cells["prodassignkey"].Value.ToString()) + "',prodassignmod='" + DateTime.Now + "' WHERE prodassignid=" + prodid1 + "";
                  cmda = conn2.CreateCommand();
                  cmda.CommandType = CommandType.Text;
                  cmda.CommandText = query601B;
                  cmda.ExecuteNonQuery();
                  conn2.Close();      
    
              }
    
          }
    
J.szareyo
  • 25
  • 8
  • Your issue seems to be: `Sett_icts_DGV_prodAssign.Rows[e.RowIndex]`. Either `Rows` is empty or `e.RowIndex` is below `0` or greater than the `Rows` property in which it can't. I would check the `e.RowIndex` first to make sure it's indeed not a negative number and or it's not more than `Sett_icts_DGV_prodAssign.Rows` before trying to access that index for that row. – Trevor Sep 07 '21 at 13:04
  • another problem though is i can't initialize my fill grid function to populate the Sett_icts_DGV_prodAssign datagridview first then call this CellValueChanged method. The whole code is inside a custom control and is triggered upon clicking by button in a mainform. – J.szareyo Sep 07 '21 at 13:20
  • The posted code is malformed and will not compile. What are the two variable definitions doing BEFORE the first open brace “{“…? `string prodid;` and `int prodid1;` should be AFTER the open brace and will cause a compile time error in its current form. In addition, I am pretty sure that `e.RowIndex` will NEVER be an invalid index considering this is coming from the grids `CellValueChanged` event. It is not possible to “change” a cells value that is “out of range” so I question the error in the first place. Please update your question with code that demonstrates this “out-of-bounds” error. – JohnG Sep 07 '21 at 21:44

0 Answers0