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 "Jrgen" a crit dans le message de news: A9362791-1244-4D98-A5B7-EAC77305B1C9@microsoft.com... >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: Mngden har ndrats. Det r inte skert att upprkningen kan > genomfras. > 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?
"Patrice" wrote: > 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 > 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.