Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:17:04+00:00 2026-06-10T23:17:04+00:00

The code I have written below will do iteration of the loop properly. It

  • 0

The code I have written below will do iteration of the loop properly. It tests the correct row against the condition, adds to the “secondary” DataTable and removes from the “master” as it is supposed to. However, upon the second iteration of the loop I get the following error:

Collection was modified; enumeration operation might not execute.

Here is the code I am using

            For Each row As DataRow In tblAgencyEdInfo.Rows
                Dim rDate As DateTime = row.Item("ExpirationDate")

                If rDate < DateTime.Now Then
                    tblExpEdInfo.ImportRow(row)
                    tblAgencyEdInfo.Rows.Remove(row)
                End If
            Next row
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:17:06+00:00Added an answer on June 10, 2026 at 11:17 pm

    You cannot modify a collection during enumeration. That means you cannot add or remove items. In this case you want to remove a DataRow from a DataTable in a For Each loop.

    The solution is either to

    1. use a For-Loop and iterate the items backwards, removing the rows via index or to
    2. use another collection (f.e. a List(Of DataRow)) and add the matching rows you want to remove in the For Each. Afterwards you just need to iterate this list and call tblAgencyEdInfo.Rows.Remove(rowToRemove)

    For example:

    Dim rowsToRemove = New List(Of DataRow)
    For Each row As DataRow In tblAgencyEdInfo.Rows
        Dim rDate As DateTime = row.Item("ExpirationDate")
        If rDate < DateTime.Now Then
            rowsToRemove.Add(row)
        End If
    Next row
    
    For Each rowToRemove As DataRow In rowsToRemove 
        tblExpEdInfo.ImportRow(row)
        tblAgencyEdInfo.Rows.Remove(row)
    Next rowToRemove 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code below.but it will print this exception and i really don't
Hi I have written below code function unique(th){ var obj = {}; for(var i
I am trying to fetch wcf service from jquery. I have written below code
I have written grammar for a language (sample code below) //this is a procedure
I have written a MapReduce program, code is below: import java.io.IOException; import java.util.Iterator; import
I have written a very simple WCF service, that worked fine (code below), then
I am have written the following code below to encode a bitarray into custom
I have written the code below for adding products to a basket, then outputting
I have written a code in java using swing.So that i will have a
I have written a code in java using swing, so that I will have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.