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"
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
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);
}
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;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I guess you could call accept changes after the method call instead of in it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER