I was trying to delete a record that was not saved in DB it throws the following exception
Error occurred. Collection was modified; enumeration operation might not execute
I can't understand why i get this error message only when try to delete that record is in memory, i don't have issue to delete a record that saved in the back end.
here is the code throws exception.
public bool DeleteVegetationZone(ref Assessment objAssessment, int VegetationZoneIDToDelete, string UserFullname, ref string ErrorMessage)
{
string RowFilter = @"VegetationZoneID=" + Convert.ToString(VegetationZoneIDToDelete);
Assessment.tblVegetationZoneRow[] VegetationZoneRows = (Assessment.tblVegetationZoneRow[])objAssessment.tblVegetationZone.Select(RowFilter);
if ((VegetationZoneRows != null) && (VegetationZoneRows.Length != 0))
{
if (VegetationZoneRows.Length == 1)
{
if (VegetationZoneRows[0].VegetationZoneID > 0)
{
VegetationZoneRows[0].UpdatedBySystemUser = UserFullname;
VegetationZoneRows[0].SaveType = (int)EnumCollection.SaveType.RemoveOnly;
}
else
{
VegetationZoneRows[0].Delete();
objAssessment.AcceptChanges();
}
//Delete the child records
//tblTransect
foreach (Assessment.tblTransectRow TransectRow in objAssessment.tblTransect.Rows)
{
if (TransectRow.VegetationZoneID == VegetationZoneIDToDelete)
DeleteVegTransectPlot(ref objAssessment, TransectRow.TransectID, UserFullname, ref ErrorMessage);
}
//tblManagementZone
foreach (Assessment.tblManagementZoneRow ManagementZoneRow in objAssessment.tblManagementZone.Rows)
{
if (ManagementZoneRow.VegetationZoneID == VegetationZoneIDToDelete)
DeleteManagementZone(ref objAssessment, ManagementZoneRow.ManagementZoneID, UserFullname, ref ErrorMessage);
}
//tblThreatenedSpeciesSubzone
foreach (Assessment.tblThreatenedSpeciesSubzoneRow ThreatenedSpeciesSubzoneRow in objAssessment.tblThreatenedSpeciesSubzone.Rows)
{
if (ThreatenedSpeciesSubzoneRow.VegetationZoneID == VegetationZoneIDToDelete)
DeleteThreatenedSpeciesSubzone(ref objAssessment, ThreatenedSpeciesSubzoneRow.ThreatenedSpeciesZoneID, UserFullname, ref ErrorMessage);
}
UpdateSpeciesGeoHabitatSurveyTime(ref objAssessment, UserFullname, ref ErrorMessage);
}
else
{
//Cannot have more than one row with same key
ErrorMessage = "Error: More than one record found - Vegetation zone ID = " + Convert.ToString(VegetationZoneIDToDelete);
return false;
}
}
else
{
//Must have at least one row with same key
ErrorMessage = "Error: Record not found - Vegetation zone ID = " + Convert.ToString(VegetationZoneIDToDelete);
return false;
}
return true;
}
exception thrown at
foreach (Assessment.tblThreatenedSpeciesSubzoneRow ThreatenedSpeciesSubzoneRow in objAssessment.tblThreatenedSpeciesSubzone.Rows)
{
if (ThreatenedSpeciesSubzoneRow.VegetationZoneID == VegetationZoneIDToDelete)
DeleteThreatenedSpeciesSubzone(ref objAssessment, ThreatenedSpeciesSubzoneRow.ThreatenedSpeciesZoneID, UserFullname, ref ErrorMessage);
}