Friday, February 17, 2012

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

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



When working with visual webpart control, my visual webpart works fine, but when i deploy the more then one Visual webpart on a single page, it gives me above error, while going in to many forums and reading the blogs, i was wondering there is some thing with my code and the SPSite and SPWeb object disposal.
i used to write the code like this
using (SPSite site =SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb())
{
// your code
}
}
though i am scoping the object rightly(using automatically dispose the object the routine gets outside the scope), but there is still some thing missing, after some time i figured out that the problem is with SPContext object rather than SPsite object, because once it dispose in one Visual Webpart, it can not be acquired back, so i change the code like this
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
// your code
}
}
and do use the using scope, it will save you from explicitly disposing the objects

No comments:

Post a Comment