This kind of exception is one of the easiest to debug. Always use the debugger. To fix, check up the collection or array for
Count
(
Length
): your index should be more or equal to 0 but less then
Count - 1
(or
Length - 1
).
In your case, the first problem which caught my eye is
dt.Rows(0)
; it
dt.Rows
has 0 rows, index 0 is out of range.
[EDIT]
After OP's clarification:
The some problem goes with the line
Convert.ToInt32(GridView1.DataKeys(gvrow.RowIndex).Value.ToString())
If, by any reason,
GridView1.DataKeys
has less or equal number of keys, than
gvrow.RowIndex
, it will be the same exception.
—SA