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 6682555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:43:11+00:00 2026-05-26T04:43:11+00:00

I am encountering an error when looping through datarows. I searched SO and tried

  • 0

I am encountering an error when looping through datarows. I searched SO and tried the solutions, but no luck.

Collection was modified; enumeration operation might not execute.

    Dim dRow As DataRow
    For Each dRow In dt.Rows
        dt.Rows.Add(dRow("CustNum"), dRow("SalesRepName"), dRow("mgrid"), "=""" & dRow("midValue") & """", dRow("dba"), dRow("sysDate"), dRow("statusID"))
    Next  

The error occurs the first time the code hits “Next“
What would be causing Collection was modified; enumeration operation might not execute.How can I resolve this error?

  • 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-05-26T04:43:12+00:00Added an answer on May 26, 2026 at 4:43 am

    You cannot enumerate a collection while updating it. Even if this code actually did work, it would run forever because you keep adding more and more rows and it would keep enumerating them.

    You can change your approach slightly and make this work by using a for loop with an index counter and a fixed upper bound.

    Something like this should work:

    Dim rowCount As Integer = dt.Rows.Count  ' Set upper bound = original row count
    Dim index as Integer
    For index = 0 To rowCount - 1  ' Iterate through the original set of rows
        Dim dRow as DataRow = dt.Rows.Item(index)  ' Get row by index
        dt.Rows.Add(dRow("CustNum"), dRow("SalesRepName"), dRow("mgrid"), "=""" & dRow("midValue") & """", dRow("dba"), dRow("sysDate"), dRow("statusID"))
    Next index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried invoking Ant programmatically, but I am encountering this error Exception in
I read SO about another user encountering similar error , but this error is
I am encountering this error when I am loading an Ad Hoc build of
I have been encountering this error for my project, which involves working with Digital
I'm encountering a very annoying error using Core Data on Mac OS X 10.5.
I'm encountering this error while compiling some old VC++ 6.0 source code. error C2632:
I am encountering this error whenever the button is clicked to collapse/expand the panel.
I am randomly encountering the following error: Message: Invalid viewstate. Client IP: xx.xxx.xxx.xx Port:
I am encountering following error on installing pykpass module from http://www.huque.com/software/pykpass/ . Following is
I am encountering the error mentioned in the title of this question. The code

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.