Thursday, 12 April 2012

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

I tried to create website which is mostly deals with SharePoint list...   So I planed to minimize the resource usage by using the 'using' statement for the SPSite and SPWeb.

I create SPSite and SPWeb where I need. 

Finally I face the error

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

 I check with the microsoft website

 http://social.technet.microsoft.com/Forums/en/sharepoint2010setup/thread/383931c6-3724-40ff-8ae8-7f26d6db1c92


They suggest don't dispose SPSite or SPWeb including SPContext.SPSite or SPContext.SPWeb also...

I remove the using statement and define the SPSite at the time on initialization by

SPSite MySite= new SPSite(SPContext.Current.WebUrl);

SPWeb MyWeb=null;

  and define the SPWeb in the constructor by 

 MyWeb=MySite.OpenWeb();

 Great now all working fine...

 Happy Coding :)