Showing posts with label SPWeb object closed disposed trying. Show all posts
Showing posts with label SPWeb object closed disposed trying. Show all posts

Tuesday, 26 June 2007

"Trying to use SPWeb object that has been closed.." Error

We were getting “Trying to use an SPWeb object that has been closed or disposed and is no longer valid.” in our MOSS -Workflow project.

After investigating all the source codes, I found the error causing statement in WebPart class.

The best practice is to dispose objects either by calling Dispose() in finally block or with using clause.

The snippet causing problem in our project was

SPGroup group;
using (SPWeb web = SPContext.Current.Web)
{
group = web.Groups[“Administrators”];
this.DisplayText = "Submit for Approval";
:
:
}

Here, the object is not really created in the memory, only the reference to the exisiting object is got and used. We should not dispose the current context web object manually in our code.

More information about best practice can be found at
http://msdn2.microsoft.com/en-us/library/ms778813.aspx

Happy Coding :)