Is this correct way of using the SPSite and SPWeb and the SPList.
private SPList list;
pulic void getList()
{
using (SPSite site = SPContext.Current.Web.Site.WebApplication.Sites[0])
{
using (SPWeb web = site.RootWeb)
{
SPListCollection lists = web.Lists;
list = lists.TryGetList("listname");
}
}
}
private override void PerformAction()
{
//retrieving items from the splist
}
I think because of this code i get an error:
Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before. Stack trace: at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData() ....
Can the cause be: that i retrieve list and then so to say dispose the SPWeb object as im using it in using clause, but the list instance is still there and i try to retrieve items from it? I do NOT perform any insert or update actions only retrieve.
And if it is the cause how can i work around that? The retrievement of the list should happen in different method, because i do an override of the method.
I transformed the code to this, as msdn says rootweb I shouldn't dispose.
using (SPSite site = new SPSite(SPContext.Current.Site.WebApplication.Sites[0].Url))
{
SPWeb web = site.RootWeb;
SPListCollection lists = web.Lists;
list = lists.TryGetList("listname");
}
Still have the same error in the logs