Answered by:
add or update a element and web object dispose

Question
-
Hey,
When I add or update a element on a list or library and dispose the web or site object it will run in an error.
SPWeb web = SPContext.Current.Web; SPList list = web.Lists[“List”]; SPListItem liItem = list.Items.Add(); ... liItem.Update(); web.Dispose();
But when I don’t dispose the web object it runs perfect.
What can I do to dispose the web object?
Thanks for your help
Stefan
Viele Grüße Stefan
Kontakt unter Info@IT-Kiessig.deSaturday, February 16, 2013 10:30 PM
Answers
-
Hello!
SPContext.Current.Web shouldn't be disposed in your code, this object is managed by the SharePoint framework. Here is an article describing the best practice in disposing SP objects.
.Net Follower (http://dotnetfollower.com)
- Proposed as answer by Christopher ClementEditor Sunday, February 17, 2013 7:04 PM
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:07 AM -
You only need to dispose of SPSite and/or SPWeb objects if you have created them yourself using the new operator.
SPContext objects are managed by the framework and should not be explicitly disposed in your code, as it may (as you have experienced) result in program failure. So you shouldn't for example put a reference to a SPContext inside a using statement like the sample below, since it will probably try to implicitly dispose of the object.
using ( SPWeb web = SPContext.Current.Web) // bad practice { } SPWeb web = SPContext.Current.Web; // good practice
Kind Regards
Bjoern
http://www.sharepointviking.com
Twitter: Follow @bjoern_rapp- Proposed as answer by Christopher ClementEditor Sunday, February 17, 2013 7:04 PM
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:21 AM -
Bjoern,
saying "you ... need to dispose ... objects ... created using the new operator", you can easily confuse people as we often use, for example, SPSite.OpenWeb() to get an SPWeb object, so in this case we don't use new operator, but we have to dispose the SPWeb object.
.Net Follower (http://dotnetfollower.com)
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:35 AM
All replies
-
Hello!
SPContext.Current.Web shouldn't be disposed in your code, this object is managed by the SharePoint framework. Here is an article describing the best practice in disposing SP objects.
.Net Follower (http://dotnetfollower.com)
- Proposed as answer by Christopher ClementEditor Sunday, February 17, 2013 7:04 PM
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:07 AM -
You only need to dispose of SPSite and/or SPWeb objects if you have created them yourself using the new operator.
SPContext objects are managed by the framework and should not be explicitly disposed in your code, as it may (as you have experienced) result in program failure. So you shouldn't for example put a reference to a SPContext inside a using statement like the sample below, since it will probably try to implicitly dispose of the object.
using ( SPWeb web = SPContext.Current.Web) // bad practice { } SPWeb web = SPContext.Current.Web; // good practice
Kind Regards
Bjoern
http://www.sharepointviking.com
Twitter: Follow @bjoern_rapp- Proposed as answer by Christopher ClementEditor Sunday, February 17, 2013 7:04 PM
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:21 AM -
Bjoern,
saying "you ... need to dispose ... objects ... created using the new operator", you can easily confuse people as we often use, for example, SPSite.OpenWeb() to get an SPWeb object, so in this case we don't use new operator, but we have to dispose the SPWeb object.
.Net Follower (http://dotnetfollower.com)
- Marked as answer by Qiao Wei Wednesday, February 20, 2013 7:47 AM
Sunday, February 17, 2013 1:35 AM