Friday, June 3, 2011

"Trying to use an SPWeb object that has been closed or disposed and is no longer valid."

I was getting the following error when my code was adding a web part to a SharePoint page:

Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

After reading this article:

http://insidesharepoint.blogspot.com/2007/07/trying-to-use-closeddisposed-webpart.html

I inspected my code and went from using this using (sorry had to write it like that):

using (SPSite spSite = SPContext.Current.Site)

and realized that I might be disposing the very site object that I'd need later on. So I changed it to this:

using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))

and it now works nicely!

No comments: