Páginas

segunda-feira, 25 de junho de 2012

When updating data inside a collection: "Collection was modified; enumeration operation may not execute".

When trying to update some data in a collection, you will find this error:
 foreach (SPListItem item in list.Items){
item["column"] = "New title";
item.Update();
}
You can´t update read-only collections. The solution: keep the ID of the ListItem you want to modify in some kind of object (like a generic list, for example), and modify this item in one-way, out of any collections, getting it by its ID.
SPListItem item = list.GetItemById(idItem);
item["column"] = "New title";
item.Update();

Nenhum comentário:

Postar um comentário