locked
i am geeting error messgae like Trying to use an SPWeb object that has been closed or disposed and is no longer valid? RRS feed

  • Question

  •  

    below is the my code. code is working fine but i am getting below error message

    Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

    please find the below code

     if (!IsPostBack)
                {
             
                    SPWeb web = SPContext.Current.Web;
                    foreach (SPList lst in web.Lists)
                    {
                        if (lst.BaseTemplate == SPListTemplateType.WebPageLibrary)
                        {
                            ddlWikis.Items.Add(lst.Title);
                        }
                    }
                    web.Dispose();
                }

     

     protected void btnReplace_Click(object sender, EventArgs e)
            {
                using (SPWeb web = SPContext.Current.Web)
                {
                    SPList list = web.Lists[ddlWikis.SelectedItem.ToString()];

                    int articleCount = 0;
                    int occurrenceCount = 0;
                    string wikiText = "";

                    foreach (SPListItem wikiItem in list.Items)
                    {
                        wikiText = wikiItem["WikiField"].ToString();
                        if (wikiText.Contains(txtFind.Text))
                        {
                            articleCount++;

                            // a trick to get the word cound
                            int count = (wikiText.Length - wikiText.Replace(txtFind.Text, "").Length) / txtFind.Text.Length;
                            occurrenceCount += count;

                            wikiItem["WikiField"] = wikiText.Replace(txtFind.Text, txtReplace.Text);
                          
                            wikiItem.Update();
                          //  web.AllowUnsafeUpdates = false;

                        }
                    }
                    lblResults.Text = articleCount.ToString() + " articles updated, " + occurrenceCount.ToString() + " occurrences updated";
                 
                }
            }

     

    please let me know what is wrong in that code.


    SWAPNA
    Monday, March 7, 2011 7:15 PM

Answers

  • It happens because you disposed SPWeb object in your code. If you do not create new SPWeb you do not need to dispose it.
    Oleg
    • Marked as answer by KeFang Chen Monday, March 14, 2011 9:15 AM
    Monday, March 7, 2011 8:15 PM
  • You dispose of the context web in the first block so it is no longer usable in the second block.

    http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx


    "SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned bySPContext.SiteSPContext.Current.SiteSPContext.Web, and SPContext.Current.Web."

    • Proposed as answer by N L V Thursday, March 10, 2011 6:12 PM
    • Marked as answer by KeFang Chen Monday, March 14, 2011 9:15 AM
    Monday, March 7, 2011 8:40 PM

All replies

  • It happens because you disposed SPWeb object in your code. If you do not create new SPWeb you do not need to dispose it.
    Oleg
    • Marked as answer by KeFang Chen Monday, March 14, 2011 9:15 AM
    Monday, March 7, 2011 8:15 PM
  • You dispose of the context web in the first block so it is no longer usable in the second block.

    http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx


    "SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned bySPContext.SiteSPContext.Current.SiteSPContext.Web, and SPContext.Current.Web."

    • Proposed as answer by N L V Thursday, March 10, 2011 6:12 PM
    • Marked as answer by KeFang Chen Monday, March 14, 2011 9:15 AM
    Monday, March 7, 2011 8:40 PM