As the title says, is it recommended to remove Data Row from Data Table while in foreach loop?
Here what I got so far –
foreach (DataRow row in dt.Rows)
{
billObj.callerID = Convert.ToInt64(row["callerID"]);
billObj.bsRespond = billObj.chargePhone(billObj);
if (!billObj.bsRespond.StartsWith("OK"))
{
dt.Rows.Remove(row);
}
}
Of course not, you’re modifying the collection that you are iterating over. Your program will blow up when you attempt to remove the row there. You cannot modify the collection while using an enumerator that was created for it otherwise it will throw an
InvalidOperationException().