Collection was modified; enumeration operation may not execute.

Topics: ASP.NET 2.0, Business Logic Layer
Jun 7, 2009 at 5:30 PM

any ideas why i got this error? (when i try to view a post.)

I did resolve this by touching the webconfig file.

PS: I'm running the BlogEngine.NET 1.4.5.12

Jun 7, 2009 at 6:16 PM

Could you tell us where are you getting this error message, the line number and the page name. It would be great if you could paste that error message here.

:Ajay

http://www.ajaymatharu.com

Jun 8, 2009 at 12:48 PM

Sorry I didn't catch the full error stack.  This has happened when I try to click on the post
i.e /post/2009/06/08/post-slug-title.aspx

The title of the error message is “Collection was modified; enumeration operation may not execute.”

The site is up and running now, if I can replicate the error I will capture the error stack and post it on this thread.

Jun 9, 2009 at 4:43 AM

If this issue is online can you give me a link to that page so that I can take a look,

:Ajay

http://www.ajaymatharu.com

Aug 17, 2010 at 1:26 PM
Edited Aug 17, 2010 at 1:27 PM

This occurs fairly often when I'm trying to delete many spam comments really quickly. If I pause a little between each, then it usually helps to prevent the error, but sometimes it even still occurs. To fix, I need to recycle the IIS app pool.

Also, I am currently using BlogEngine.NET v1.5.0.7

Here's the error info:

Server Error in '/' Application.

Collection was modified; enumeration operation may not execute.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Source Error:

Line 157:		private List<IPublishable> SearchForPosts()
Line 158: {
Line 159: return Search.FindRelatedItems(this.Item);
Line 160: }
Line 161: }


Source File: e:\web\pietschsof1\htdocs\App_Code\Controls\RelatedPosts.cs    Line: 159

Stack Trace:

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +51
BlogEngine.Core.Search.BuildCatalog() +439
BlogEngine.Core.Search..cctor() +96

[TypeInitializationException: The type initializer for 'BlogEngine.Core.Search' threw an exception.]
BlogEngine.Core.Search.FindRelatedItems(IPublishable post) +0
Controls.RelatedPosts.SearchForPosts() in e:\web\pietschsof1\htdocs\App_Code\Controls\RelatedPosts.cs:159
Controls.RelatedPosts.RenderControl(HtmlTextWriter writer) in e:\web\pietschsof1\htdocs\App_Code\Controls\RelatedPosts.cs:99
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.themes_inove_site_master.__RenderForm1(HtmlTextWriter __w, Control parameterContainer) in e:\web\pietschsof1\htdocs\themes\inove\site.master:28
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +8676409
System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32
System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
BlogEngine.Core.Web.Controls.BlogBasePage.Render(HtmlTextWriter writer) +39
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266



Version Information: Microsoft .NET Framework Version:2.0.50727.3607; ASP.NET Version:2.0.50727.3082

Aug 17, 2010 at 1:31 PM

Wow, I just got a new exception thrown while deleting multiple spam comments:

Server Error in '/' Application.

Violation of PRIMARY KEY constraint 'PK_be_PostComment'. Cannot insert duplicate key in object 'dbo.be_PostComment'.
The statement has been terminated.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_be_PostComment'. Cannot insert duplicate key in object 'dbo.be_PostComment'.
The statement has been terminated.

Source Error:

Line 259:			if (comment.Id == new Guid(Request.QueryString["deletecomment"]))
Line 260: {
Line 261: Post.RemoveComment(comment);
Line 262:
Line 263: int index = Request.RawUrl.IndexOf("?");


Source File: e:\web\pietschsof1\htdocs\User controls\CommentView.ascx.cs    Line: 261

Stack Trace:

[SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_be_PostComment'. Cannot insert duplicate key in object 'dbo.be_PostComment'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +204
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
BlogEngine.Core.Providers.DbBlogProvider.UpdateComments(Post post, DbConnection conn, DbProviderFactory provider) +1566
BlogEngine.Core.Providers.DbBlogProvider.UpdatePost(Post post) +1555
BlogEngine.Core.Providers.BlogService.UpdatePost(Post post) +23
BlogEngine.Core.Post.DataUpdate() +13
BlogEngine.Core.Post.RemoveComment(Comment comment) +66
User_controls_CommentView.DeleteComment() in e:\web\pietschsof1\htdocs\User controls\CommentView.ascx.cs:261
User_controls_CommentView.Page_Load(Object sender, EventArgs e) in e:\web\pietschsof1\htdocs\User controls\CommentView.ascx.cs:125
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Control.LoadRecursive() +141
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627



Version Information: Microsoft .NET Framework Version:2.0.50727.3607; ASP.NET Version:2.0.50727.3082