Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old February 16th, 2011, 08:19 PM
Registered User
Join Date: Jan 2011
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Smile Exception error "Collection was modified; enumeration operation may not execute"

Hi folks.

For anyone out there who tried to exercise the delete action method when the user is already authenticated.

In case you have succeeded after all printing errors in the book and tries to follow the advice in the last paragraph of page 116 in Chapter 1: "We can repeat the same steps for the Delete action methods within our Controller to lock down permission to delete Dinners as well, and ensure that only the host of a Dinner can delete it."

You will find that the code defined for Delete a dinner in the repository (DinnerRepository) will generate an exception "Collection was modified; enumeration operation may not execute" error.

This is due to the fact that as you delete the objects from the context, the Entity Framework is actively updating the RSVPs navigation property count which means the "dinner.RSVPs" collection is being changed during the "foreach" loop which will always cause the exception you are getting.

So the are 2 possible solutions:

a) Change the current foreach parameters:

public void Delete(Dinner dinner)

//--- Instead of using "foreach ( var rsvp in dinner.RSVPs )" ---//
//--- use this: ---//
foreach ( var rsvp in dinner.RSVPs.ToList() )
entities.RSVPs.DeleteObject( rsvp );



b) Use the LINQ ForEach method instead:

public void Delete(Dinner dinner)

//--- Instead of using "foreach ( var rsvp in dinner.RSVPs )" ---//
//--- use this: ---//
dinner.RSVPs.ToList().ForEach(record =>


Hope it helps (at least it worked for me )

The Following User Says Thank You to caof2005 For This Useful Post:
BrennanFuchs (April 6th, 2011)
  #2 (permalink)  
Old April 6th, 2011, 08:26 PM
Registered User
Join Date: Apr 2011
Location: windsor ns
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via Skype™ to BrennanFuchs

Thanks mate... VERY green to both C# and MVC heck still stuck on 2.0 at work so even the framework is newish. Knew why the error was occurring not idea what the solution was.

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 23rd, 2009 12:12 AM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 02:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 05:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 04:09 PM

All times are GMT -4. The time now is 06:20 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.