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

    Question

  •  We are running SP2010. I am not a developer and have not touched any code, please would someone tell me how to fix this. I am unable to create status lists and project tasks. I have tried to delete the affected web parts and also the site that seems to have been affected the most.
    • Moved by Mike Walsh FIN Thursday, March 17, 2011 10:44 AM Off-topic SP 2010 post in pre-SP 2010 forum (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    Thursday, March 17, 2011 10:24 AM

Answers

  • You may never close the objects SPContext.Current.Web or SPContext.Current.Site because these are the instances your sharepoint is running on.

    On the other hand, if you create your own instances of SPWeb or SPSite, you must dispose of them.

    There is a tool developed by microsoft (SPDisposeCheck) that checks your disposing habits and gives you a nice report of possible issues: http://archive.msdn.microsoft.com/SPDisposeCheck

     

    Regards,

    Jeroen


    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    • Proposed as answer by Nik Patel Friday, March 18, 2011 2:59 PM
    • Marked as answer by Rock Wang– MSFT Friday, March 25, 2011 7:51 AM
    Friday, March 18, 2011 2:56 PM

All replies

  • Have you managed to remove any custom web parts by appending ?contents=1 to the url

    i.e http://localhost/sites/pages/default.aspx?contents=1

    This should help you clear up any problematic web parts

    Tech bit: This quite often occurs is someone has disposed of a SPWeb object taken from SPContext.Current.Web in their code.


    If you found this post helpful, please mark it so. Thanks!

    Lightning Tools – where I work
    BCS Tester Man – My Codeplex personal project

    • Proposed as answer by Bala Chowdary Friday, May 02, 2014 5:42 AM
    Thursday, March 17, 2011 10:55 AM
  • Hi,

    Are you trying to do it as 

    using (SPWeb spweb = SPContext.Current.Web)
        {

         ....
        }

    this will give an above stated exception. not sure of reason but you can avoid this error if you go like

    using (SPSite spsite = new SPSite(SPContext.Current.site.url))
        {

            using (SPWeb spweb = spsite.openweb())
             {

              ....
             }

        }

    Hope this helps.

    • Proposed as answer by vijay sathe Tuesday, October 18, 2011 11:50 AM
    Thursday, March 17, 2011 11:07 AM
  • Amol,

    Why would you want to instatiate a new SPSite when you have it already in SPContext?

    You would just use: SPContext.Current.Site.OpenWeb() or SPContext.Current.Web

    That's what it's there for, not to use it's properties to instantiate a new class, that's pointless.


    If you found this post helpful, please mark it so. Thanks!

    Lightning Tools – where I work
    BCS Tester Man – My Codeplex personal project

    Thursday, March 17, 2011 11:10 AM
  • Hi Phill,

    I came across the same error when i was trying to user SPContext.Current.Web for creating new object of spweb in using stetment. i used above approach and got my code working just fine.

    i thing, this error might be because we are not creating new instance of spweb in "SPWeb spweb = SPContext.Current.Web" and using will dispose the spweb object once control comes out of its scope. i am not sure about use of SPContext.Current.Site.OpenWeb(). correct me if i am missing some thing.

    Friday, March 18, 2011 2:39 PM
  • You may never close the objects SPContext.Current.Web or SPContext.Current.Site because these are the instances your sharepoint is running on.

    On the other hand, if you create your own instances of SPWeb or SPSite, you must dispose of them.

    There is a tool developed by microsoft (SPDisposeCheck) that checks your disposing habits and gives you a nice report of possible issues: http://archive.msdn.microsoft.com/SPDisposeCheck

     

    Regards,

    Jeroen


    My blog on WSS / MOSS development is found at http://jebass.blogspot.com
    • Proposed as answer by Nik Patel Friday, March 18, 2011 2:59 PM
    • Marked as answer by Rock Wang– MSFT Friday, March 25, 2011 7:51 AM
    Friday, March 18, 2011 2:56 PM
  • Agreed with Jeroen.. I think your code is trying to access the SPSite or SPweb object which was disposed. If you have code with SPContext, do not dispose the SPWeb or SPSite objects. You can't dispose the SPcontent objects. Try to look for that code, fix it, and redeploy and you should be OK..

    Nik

    Friday, March 18, 2011 3:01 PM
  • I agree that we cant dispose the object SPContext.current.Web or SPContext.Current.Site directly. I got exactly same error and the changes i made to resolve it worked fine for me.

    Again that might be the one of the reasons of getting this error and might not the only reasone. if you are facing this error because of this same thing you can try it and check the results.

    Morever when you say SPWeb spWeb= SPContext.Current.Web we are not creating a new instance of spweb, we are refering to the existing instace of the web object which is already there in SPContext.

    if you use using (SPWeb spWeb= SPContext.Current.Web), using will dispose the spWeb object after control goes out of its scope. here catch is, you are disposing an object which is not created by you. this might be causing the problem. As i believe SPContext object is specific with each http request made to server, so runtime or frame work will try to dispose this object once current request is served and here we are getting in trouble.

    this explaination might not be correct but is what i think the probable reason of getting this error

    Saturday, March 19, 2011 7:15 AM
  • Yes Your  are right many thanks .

     

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

                {

                    using (SPWeb sharePointWeb = site.RootWeb)

                    {

               }

           }

     

     

    Tuesday, November 15, 2011 10:51 AM
  • Thanks Amol, I think when using (SPWeb spweb = SPContext.Current.Web) the compiler takes the current instance of the running SPContext.Current.Web object 

    and disposes it in the end of using statement. Doing the way you suggested by creating a new instance using (SPSite spsite = new SPSite(SPContext.Current.site.url))

    only disposes the instance of newly created SPSite, which is perfect as we dont want to dispose our SPContext.Current.Web object.

    Cheers

    Wednesday, May 16, 2012 10:27 AM
  • just remove the using statement, this seems more logical rather than creating a new instance of the objects?

    san

    Friday, July 27, 2012 2:47 PM
  • Both of these methods calls will throw the error.

    Monday, October 08, 2012 12:40 AM