locked
Sharepoint objects not disposing RRS feed

  • Question

  • Hi,

    I am trying to make my list columns readonly and when i deploy this code i am seeing warning in the logs that i am not disposing the objects correctly.
    Any thoughts?


    protected void ListColumnsReadOnlyAsTrue()

    {


    SPWeb objWeb=null;


    try

    {

    objWeb = new SPSite(SPContext.Current.Web.Url).OpenWeb();


    SPList list = objWeb.Lists.TryGetList("BulkDataCorrection");


    if (list != null)

    {

    objWeb.AllowUnsafeUpdates = true;

    list.Fields["CostMethod"].ReadOnlyField = true;

    list.Fields["CostMethod"].Update();

    list.Fields["CostPackageName"].ReadOnlyField = true;

    list.Fields["CostPackageName"].Update();

    list.Fields["Placement"].ReadOnlyField = true;

    list.Fields["Placement"].Update();

    list.Fields["StartDate"].ReadOnlyField = true;

    list.Fields["StartDate"].Update();

    list.Fields["EndDate"].ReadOnlyField = true;

    list.Fields["EndDate"].Update();

    list.Fields["ReportedImpressions"].ReadOnlyField = true;

    list.Fields["ReportedImpressions"].Update();

    list.Fields["ReportedClicks"].ReadOnlyField = true;

    list.Fields["ReportedClicks"].Update();

    list.Fields["ReportedCost"].ReadOnlyField = true;

    list.Fields["ReportedCost"].Update();

    list.Fields["EditedByUser"].ReadOnlyField = true;

    list.Fields["EditedByUser"].Update();

    list.Fields["ProcessedFlag"].ReadOnlyField = true;

    list.Fields["ProcessedFlag"].Update();

    list.Fields["ModifiedDate"].ReadOnlyField = true;

    list.Fields["ModifiedDate"].Update();

    list.Update();objWeb.AllowUnsafeUpdates = false;

    }

    }


    catch (Exception ex)

    {Exceptions.HandleException(ex);

    showAlert(ex.Message);

    }


    finally

    {

    if (objWeb != null)

    objWeb.Dispose();

    }

    }


    • Edited by Sasha V Saturday, February 18, 2012 6:42 AM
    Saturday, February 18, 2012 6:34 AM

Answers

  • Try like this

    protected void ListColumnsReadOnlyAsTrue()
     
    {
     
    
    
     
    
    try
     
    {
     
    using(SPWeb objWeb = new SPWeb(SPContext.Current.Web.ID))
    {
       // Your code
    }
     
    
    catch (Exception ex)
     
    {Exceptions.HandleException(ex);
     
    showAlert(ex.Message);
     
    }
     
    
     
    }
    

    using will handle disposing for you.
    • Proposed as answer by Sjoukje ZaalMVP Saturday, February 18, 2012 7:13 AM
    • Marked as answer by Sasha V Sunday, February 19, 2012 5:15 AM
    Saturday, February 18, 2012 7:09 AM

All replies

  • Try like this

    protected void ListColumnsReadOnlyAsTrue()
     
    {
     
    
    
     
    
    try
     
    {
     
    using(SPWeb objWeb = new SPWeb(SPContext.Current.Web.ID))
    {
       // Your code
    }
     
    
    catch (Exception ex)
     
    {Exceptions.HandleException(ex);
     
    showAlert(ex.Message);
     
    }
     
    
     
    }
    

    using will handle disposing for you.
    • Proposed as answer by Sjoukje ZaalMVP Saturday, February 18, 2012 7:13 AM
    • Marked as answer by Sasha V Sunday, February 19, 2012 5:15 AM
    Saturday, February 18, 2012 7:09 AM
  • I think in your code you are creating an SPSite object temporarily to create a sp web object. So even though you dispose the spweb object the spsite object is not disposed.

    You should use the following code:

    using(SPWeb objWeb = new SPWeb(SPContext.Current.Web.ID))
    {
       // Your code
    }


    Varun Malhotra
    =================
    If my post solves your problem could you mark the post as Answered or Vote As Helpful if my post has been helpful for you.

    Saturday, February 18, 2012 7:25 AM