Link to home
Start Free TrialLog in
Avatar of Sha1395
Sha1395

asked on

Error occurred. Collection was modified; enumeration operation might not execute

I have following piece of code in my project "foreach loop" throws the following excepiton "
Error occurred. Collection was modified; enumeration operation might not execute"

foreach (Assessment.tblMitchellLandscapeIDRow MitchellRow in objAssessment.tblMitchellLandscapeID.Rows)
{                                
     if (MitchellRow.AssessmentVersionID == AssessmentVersionID)
     DeleteMitchellLandscape(ref objAssessment, MitchellRow.MitchellID, UserFullname, ref ErrorMessage);
}

Open in new window


Delete mitchell landscape calls the following  but not sure why the foreach loop throws that exception. Some one please correct me what's the right way to handle this

        public bool DeleteMitchellLandscape(ref Assessment objAssessment, int MitchellIDToDelete, string UserFullname, ref string ErrorMessage)
        {
            string RowFilter = @"MitchellID=" + MitchellIDToDelete;
            Assessment.tblMitchellLandscapeIDRow[] mitchellLandscaperIdRowsows = (Assessment.tblMitchellLandscapeIDRow[])objAssessment.tblMitchellLandscapeID.Select(RowFilter);
            if ((mitchellLandscaperIdRowsows != null) && (mitchellLandscaperIdRowsows.Length != 0))
            {
                if (mitchellLandscaperIdRowsows.Length == 1)
                {
                    if (mitchellLandscaperIdRowsows[0].MitchellID > 0)
                    {
                        mitchellLandscaperIdRowsows[0].UpdatedBySystemUser = UserFullname;
                        mitchellLandscaperIdRowsows[0].SaveType = (int)EnumCollection.SaveType.RemoveOnly;

                    }
                    else
                    {
                        mitchellLandscaperIdRowsows[0].Delete();
                        objAssessment.AcceptChanges();
                    }
                }
                else
                {
                    //Cannot have more than one row with same key
                    ErrorMessage = "Error: More than one record found - Mitchell Landscape ID = " + Convert.ToString(MitchellIDToDelete);
                    return false;
                }
            }
            else
            {
                //Must have at least one row with same key
                ErrorMessage = "Error: Record not found - Mitchell Landscape ID = " + Convert.ToString(MitchellIDToDelete);
                return false;
            }
            return true;
        }

Open in new window

SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sha1395
Sha1395

ASKER

i agree but if i remove "objAssessment.AcceptChanges();" this piece of code from deletemitchelllandscape func , i don't have problem to execute the foreach loop and i can acceptChanges after foreach. What i am doing is correct or not i am not sure but solve my purpose to avoid throw exception but i want to learn the right way of approach.
I guess you could call accept changes after the method call instead of in it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial