user1712552 user1712552 - 6 months ago 64
Vb.net Question

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

I have a gridview which when I upload my data and make an attempt to submit it to mysql database I get his error

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

Here is the code I am using, and the problem occurs on the address row.

For i = 0 To GridView1.Rows.Count - 1
Using sqlCommand As New MySqlCommand()

student_id = GridView1.Rows(0).Cells(i).Text.ToString
age = GridView1.Rows(1).Cells(i).Text.ToString
adress = GridView1.Rows(2).Cells(i).Text.ToString

Answer

Something seems wrong here.
You loop over the rows of the gridview, but then you take the index of the row to access the cells.
Probably you want this

For i = 0 To GridView1.Rows.Count - 1 
    Using sqlCommand As New MySqlCommand() 
       .... 
       student_id = GridView1.Rows(i).Cells(0).Text.ToString 
       age = GridView1.Rows(i).Cells(1).Text.ToString 
       adress = GridView1.Rows(i).Cells(2).Text.ToString 
       ....
    End Using
Next