5 Replies - 618 Views - Last Post: 27 July 2012 - 07:56 AM Rate Topic: -----

#1 tendaimare  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 161
  • Joined: 04-November 10

Index out of range exception

Posted 27 July 2012 - 04:16 AM

I am using the code I have shown below to loop through my datatable in search of certain values and when i find those values I remove them from the datatable. I am using the below to do this:

 If myTable.Rows.Count = 0 Then

            Else
                'Try
                For Each myDataRow As DataRow In dTable.Rows
                    For myArraySearch As Integer = 0 To 50 Step 1
                        dTable.Rows.RemoveAt(dTable.Rows.IndexOf(dTable.Rows.Find(myArray(myArraySearch))))
                    Next
                Next
                'Catch ex As Exception
                'End Try
            End If




When I remove the try catch statement I get the error "There is no row at position -1.". Is there a way to contain this error without having to use the try catch statement.

Is This A Good Question/Topic? 0
  • +

Replies To: Index out of range exception

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3044
  • View blog
  • Posts: 4,556
  • Joined: 08-June 10

Re: Index out of range exception

Posted 27 July 2012 - 05:11 AM

Yes. Your mistake there is to assume that the IndexOf() call will return a valid index. If the myArray(myArraySearch) object can not be found among the rows of the dTable, then the Find() call will return a null, which in turn means that IndexOf(Find()) returns -1. That's what's causing the exception, because RemoveAt() call is expecting a valid, positive integer, but it's getting -1.

You can easily test for that using a simple IF statement. Check the value of the IndexOf() call, and make sure it's not -1 before you call RemoveAt()
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 1949
  • View blog
  • Posts: 8,675
  • Joined: 29-May 08

Re: Index out of range exception

Posted 27 July 2012 - 05:22 AM

Still not going to work, even if you did that. As you'll change the enumerator on .Rows
Was This Post Helpful? 1
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3044
  • View blog
  • Posts: 4,556
  • Joined: 08-June 10

Re: Index out of range exception

Posted 27 July 2012 - 05:37 AM

Good point. Assuming there are actually 50 myArray elements, removing the outer loop altogether should fix that though.
Was This Post Helpful? 1
  • +
  • -

#5 tendaimare  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 161
  • Joined: 04-November 10

Re: Index out of range exception

Posted 27 July 2012 - 05:48 AM

thanks Atli and AdamSpeight2008. I resorted to using this code instead

  For Each key In myArray
                    Dim row = dTable.Rows.Find(key)

                    If row IsNot Nothing Then
                        dTable.Rows.Remove(row)
                        'row.Delete()
                    End If
                Next



Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 1949
  • View blog
  • Posts: 8,675
  • Joined: 29-May 08

Re: Index out of range exception

Posted 27 July 2012 - 07:56 AM

I was slightly incorrect about it modifying the collection, it won't change until you do. I'd forgot it involved a two part process;-
  • Generates a list of change want to happen to data
  • Accept (& apply) those changes.

dTable.AccectChanges()


so using a LINQ query to get the rows, which you want to remove.

Dim re = From r in dTable.Rows
         Where myArray.Contains(r)
Foreach e in re
 e.Delete()
Next
dTable.AcceptChanges()

This post has been edited by AdamSpeight2008: 27 July 2012 - 04:57 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1