Saving of deleted rows

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with master/child tables. I Deleted a row in the child table
with datarow.Delete and tried to save the change to the database. Because I
have a Foreign Key constraint between the tables I must make updates in a
certain order
1. Child Deletes
2. Master Updates/Deletes/Inserts
3. Child Updates/Inserts

When I try to Save the Child Deletes to the database via the dataadapter i
get the following error:

InvalidOperationException:
The Collection has changed
in swedish: Mängden har ändrats. Det är inte säkert att uppräkningen kan
genomföras.
vid System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
Det finns ingen rad på positionen 0.

I tried a for each datarow in datatable loop and the exception is thrown at
the next command after the first datarow is saved.

It seems that you can't move to the next row in the collection after the
deleted row is saved to the database.

Does anybody have a workaround for this problem?
 
Likely because you can't browse a collection with "for each" if you change
the collection.

The usual way to handle this is to use the index from count-1 to 0 so that
deleting an element doesn't make you skip over an element.

As a side note (and if I remember) you can also use the Update method on the
datatable so that you don't have to browse for records but sill handles in
which order tables are updated...
 
Patrice said:
Likely because you can't browse a collection with "for each" if you change
the collection.

The usual way to handle this is to use the index from count-1 to 0 so that
deleting an element doesn't make you skip over an element.

As a side note (and if I remember) you can also use the Update method on the
datatable so that you don't have to browse for records but sill handles in
which order tables are updated...
In my original code that caused the exeption the save function in my dalc it
is boiled down to DataAdapter.Update(DataTable) and this still causes the
InvalidOperationException.
 
Back
Top