Archive

Archive for April, 2011

Visual web part error: Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0×80030102(STG_E_REVERTED))

April 23, 2011 1 comment

Yesterday I was working with visual webpart , While development my visual webpart works as expected, but when i deployed the more then one instance of Visual webparts on a page, it gives me error “Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0×80030102(STG_E_REVERTED))”, while searching about this and reading on MSDN, i was wondering there is some thing wrong with my code and the related to SPSite or SPWeb object disposal.

In my web part I used followig code
I used to write the code like this

using (SPSite _site = SPContext.Current.Site)
{
  using (SPWeb _web = site.RootWeb)
  {
   // your code
  }
}

To correct this issue use code like following in my web part

SPSite currSite = SPContext.Current.Site;
SPWeb currWeb = currSite.RootWeb;
using (SPSite eSite = new SPSite(currSite.ID))
{
	using (SPWeb eWeb = eSite.OpenWeb(currWeb.ID))
	{
	   //Your code
	}
}

Happy coding…

Advertisements