Sharepoint Object disposal

Category: sharepoint 2010 programming

Question

RK Senthil on Sun, 04 Nov 2012 03:13:02


Hi

In the below code can i use the results list item collection outside the using block after disposing the web.


SPListItemCollection results = null;
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
 using (SPWeb web = site.OpenWeb())
  {
    SPList list = web.GetList(string);
    SPQuery query = new SPQuery();
    results = list.GetItems(query);                   
   }
}

foreach (SPListItem item in results)

{

}

                                                                                                                                            

        

Replies

varun.atluri on Sun, 04 Nov 2012 07:32:36


Yes we can I have tried your code 

SPListItemCollection results = null;
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.GetList("Shared Documents");
                        SPQuery query = new SPQuery();
                        results = list.GetItems(query);                    
                    }
                }
                foreach (SPListItem item in results)
                {
                }

I have seen a List Item.. no problem with the iteration...

SekThang on Sun, 04 Nov 2012 17:58:34


Hi Senthil,

 Even I have tried your code. I don't think so there would be any problem with the iteration.

 

RK Senthil on Wed, 07 Nov 2012 19:37:10


No, i am getting the below issue in the log.

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.

I think i should not dispose the site and web objects until i finish using the result object here. Please suggest.


Nicolas Castel on Wed, 07 Nov 2012 19:43:28


Hi,

Why don't use directly SPContext.Current.Web? (if you use it, Do not dispose it!)

SPList list = SPContext.Current.Web.GetList("Shared Documents");
SPQuery query = new SPQuery();
results = list.GetItems(query);                    
foreach (SPListItem item in results)
{
}

NicoBzh

varun.atluri on Wed, 07 Nov 2012 20:13:24


As we have declared " SPListItemCollection results = null; " statement outside of the using block you are getting the issue. As using statement will dispose the objects with it closed. For optimised best solutions we need to use all objects before disposing using block. 

Use:

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.GetList("Shared Documents");
                        SPQuery query = new SPQuery();

                        SPListItemCollection results = list.GetItems(query);                    

			foreach (SPListItem item in results)
                	{
                             //Do your stuff here 
                	}

                    }
                }