I kept getting this error: "Trying to use an SPWeb object that has been closed or disposed and is no longer valid."
Stack trace: at Microsoft.SharePoint.WebPartPages.SPWebPartManager.get_Web()
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ActivateConnections()
at System.Web.UI.WebControls.WebParts.WebPartManager.OnPageLoadComplete(Object sender, EventArgs e)
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Page.OnLoadComplete(EventArgs e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
The scenario was a simple list with a checkbox that was causing the page to reload. For some reason, sometimes it worked, and sometimes it was throwing this error.
After reading this great post, I figured out what the issue was. The using() directive was causing the SPWeb object to be disposed before all code was executed:
http://www.theartofsharepoint.com/2007/06/trying-to-use-spweb-object-that-has.html
I commented out the shit I no longer needed, and here's an example of the result:
protected void LoadClip(yadda)
{
... do some logic ...
// using (SPWeb ThisWeb = SPContext.Current.Web)
//{
SPWeb ThisWeb = SPContext.Current.Web;
ThisTable = ThisWeb.GetSiteData(q);
//}
... and more code here with reference to ThisWeb ...
}
here's a few more links that were helpful:
http://blogs.msdn.com/rogerla/archive/2009/01/14/try-finally-using-sharepoint-dispose.aspx
http://msdn.microsoft.com/en-us/library/aa973248.aspx