table row count not updated in "while" loop?

  • Thread starter Thread starter Martin.McDonald
  • Start date Start date
M

Martin.McDonald

while (MyModel.MyTable.Rows.Count > 0)
{
MyModel.MyTable[0].Delete();
}

This will execute indefinitely. One must add "AcceptChanges" after the
delete to fix it. Is this a bug or is it by ADO.Net design?
--Marty
 
I think I've resolved this... I'll likely use a "for" loop and delete
rows whose rowstate is not "deleted".
 
I think I've resolved this... I'll likely use a "for" loop and delete
rows whose rowstate is not "deleted".

Delete() just marks a row. Have you tried Remove() instead?
 
No, this is by design.

Delete marks a row to be deleted next time you call Update on your data
adapter.

AcceptChanges basically says to 'commit' all the current edits to the
datatable, and mark all the rows as unchanged. This does not do anything
with the database - just to the in memory datatable.

To actually get rid of a row, you would call Remove. Remove physically
removes the row - which would then not be there when you call Update, since
Update looks for rows marked for deletion when it is ready to delete rows.
 
Back
Top