Results 1 to 5 of 5

Thread: [RESOLVED] DATAROW Looping / Deleting

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    [RESOLVED] DATAROW Looping / Deleting

    Alright, I have this piece of code:

    vb Code:
    1. Assignments_SQLSelect()
    2.             Adapter.DeleteCommand = New OleDbCommand("DELETE FROM Assignments WHERE CIndex = @CIndex")
    3.             Adapter.DeleteCommand.Connection = Connection
    4.             Connection.Open()
    5.             With Adapter.DeleteCommand
    6.                 .Parameters.Add("@CIndex", OleDbType.Integer, 5, "CIndex")
    7.             End With
    8.             For Each DRow As DataRow In DS.Tables(0).Rows
    9.                 If DRow.Item("Student").ToString.Contains(.lstNames.Items.Item(.RIndex).Text) Then
    10.                     DRow = DS.Tables(0).Rows(.RIndex)
    11.                     DRow.Delete()
    12.                 End If
    13.             Next
    14.             Adapter.Update(DS)
    15.             DS.AcceptChanges()
    16.             Connection.Close()

    So what its suppose to do is Loop Through each Row in the Database and then IF the DRow Contains the .RIndex.Text Item then it will Delete (This is Fine, I did a Debug to see if it was finding the items & doing the IF Statment (and it Did)

    Problem: Its only deleting the First Row that Contains that Item, not the Rest. So.. how do I fix this? I tried putting Adapter.Update and DS.ACcept in the LOOP statement but came up with a ERROR.
    Last edited by Wesley008; Nov 10th, 2008 at 04:36 PM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Looping through Each Item in DataRow and Deleting if It Contains * Text

    The problem is with the type of loop you're using... Don't use For Each loop. Use the regular For loop with a loop counter and looping it backwards
    Code:
    Dim DRow As DataRow = Nothing
    For i As Integer = DataRow In DS.Tables(0).Rows - 1 To 0 Step -1
          DRow = DS.Tables(0).Rows(i)
          If DRow.Item("Student").ToString.Contains(.lstNames.Items.Item(.RIndex).Text) Then
               DRow.Delete()                
          End If            
    Next
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Looping through Each Item in DataRow and Deleting if It Contains * Text

    Why should I be using the For Loop instead of a For Each Loop ? Aren't they the same?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Looping through Each Item in DataRow and Deleting if It Contains * Text

    When you use a For Each loop, it internally obtains an enumeration of the items in the collection. Then in your loop, you delete an item, which causes the enueration of the items to change (for example, you delete item0, so item1 is now moved up to become item0) while the loop is still having the old enumeration of the items.
    To get around this, you have to use a loop counter and looping backwards, so even the last item was deleted it won't affect the enumeration for the next item.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: Looping through Each Item in DataRow and Deleting if It Contains * Text

    Alright Thank You for the Explaination.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width