ADO.NET Pitfalls- Delete record from datatable while looping through
foreach(DataRow r in SampleDataTable.Rows){
if(condition)
r.Delete()
}
this works only if DataTable rows collection rows has rowstate unchaged (i.e. you’ve filled the datatable through dataadapter Fill method).
If rowstate is Added then r.Delete() removes the record and throws exception as looping through the collection that “Collection may be modified”.
Comments