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

Mar 18, 2010 at 11:30 AM

Hello,

Thanks for this tool, it's amazing.

But I notice one bug.
Sometime, i got this error :

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

So i've looked into the source code and I see :

using (SPSite site = SPContext.Current.Site)

using (SPWeb webSite = SPContext.Current.Web)

 

But as said by Microsoft in their Best Practice for Dispose (http://msdn.microsoft.com/en-us/library/aa973248.aspx) :

SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.

 

Maybe this can explain the error...

Regards,
Pierre-Alexandre

May 13, 2010 at 8:27 AM

Hello Pierre,

I am facing this same issue... did you have any luck with your setup? Did you find a solution on how to get this feature working?

Thanks, Sujit.

May 18, 2010 at 10:52 AM
Edited May 18, 2010 at 10:52 AM

Hello,

I don't modify the feature because i don't use it anymore.

But you can try to remove using statments : :

ie replace :

 

using (SPSite site = SPContext.Current.Site)
{
     ...
     using (SPWeb web = SPContext.Current.List.ParentWeb)
     {
          ...
     }
}

 

by :

 

SPSite site = SPContext.Current.Site
...
SPWeb web = SPContext.Current.List.ParentWeb
...

 

 

Pierre-Alexandre