SharePoint 2010 Application Page Code Behind Throws Exception “Trying to use an SPWeb object that has been closed or disposed and is no longer valid.”

This was the situation:

I have a SharePoint Foundation 2010 project in VS2010. There I deploy some SharePoint lists with custom views an custom form pages.

In one case I wanted to override the RaisePostBackEvent method of the customized form page. In the method I wanted to do some actions before the Save button code is executed. (Its the standard Save button of a SharePoint list form: <SharePoint:SaveButton...>.

The overridden RaisePostBackEvent locked like this:

1: protected  override  void  RaisePostBackEvent(System.Web.UI.IPostBackEventHandler  sourceControl, string  eventArgument)
2:         {
3:             if  (sourceControl is  System.Web.UI.WebControls.Button )
4:             {
5:                 System.Web.UI.WebControls.Button  button = ((System.Web.UI.WebControls.Button )sourceControl);
6:                 if  (button.ClientID.StartsWith(savebutton1.ClientID) || button.ClientID.StartsWith(savebutton2.ClientID))
7:                 {
8:                     using  (SPWeb  web = SPContext .Current.Web)
9:                     {
10:                         //some code 
11:                     }
12:                 }
13:             };
14:
15:             base .RaisePostBackEvent(sourceControl, eventArgument);
16:         }
17:
18:

When executing the custom list form page there was no errors when I filled all field input boxes with data and press the Save button. But as I tried the “required field” functionality – leaving some fields blank – the SharePoint system throws the following error message in the SharePoint log:

06/15/2010 18:11:19.94 	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	Microsoft.SharePoint.SPException: Trying to use an SPWeb object that has been closed or disposed and is no longer valid.    at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ThrowIfManagerIsInvalid()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.get_LimitedWebPartManager()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadConnectionsState()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadWebParts()     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.OnPageInitComplete(Object sender, EventArgs e)     at System.EventHandler.Invoke(Object sender, EventArgs e)     at System.Web.UI.Page.OnInitComplete(EventArgs e)     at Sal...	b196095c-b664-4f3c-b764-f9b3ef8ac5db
06/15/2010 18:11:19.94*	w3wp.exe (0x1384)                       	0x0E5C	SharePoint Foundation         	Runtime                       	tkau	Unexpected	...esAppLists.Layouts.SalesApp.AddDecisionMakerForm.OnInitComplete(EventArgs e)     at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)	b196095c-b664-4f3c-b764-f9b3ef8ac5db
0

After some debugging and thinking about it I realized that it only happens when the I press the Save button and this action will not close the dialog frame of the list form page. This means: The postback action does more than “store data and leave the dialog frame”.

So I figured out that there is a overridden RaisePostBackEvent method on the page.

I removed the method for testing purpose… and the problem was away!

It seems to me that it is not possible to use using (SPWeb web = SPContext.Current.Web) in such a method.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.