2

How should change a code to use another conect to site in Sharepoint. Which code, to change SPWeb witryna = SPContext.Current.Web;

Here is my examplary code:

           protected void Button3_Click(object sender, EventArgs e)
            {
                SPWeb witryna = SPContext.Current.Web;
                SPList oList = witryna.Lists["Szczegoly"];
               // SPListItemCollection elementyDoUsuniecia = oList.Items;
                string FullQuery = "<Where><And><Eq><FieldRef Name='{0}' /><Value Type='DateTime'>{1}</Value></Eq><Eq><FieldRef Name='Pracownik' /><Value Type='Text'>{2}</Value></Eq></And></Where>";
                string dateISO = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(TextBox4.Text).ToUniversalTime().AddHours(2.0));
                var query1 = new SPQuery();
                query1.Query = String.Format(FullQuery, "Dzien", dateISO, DropDownList1.SelectedItem.Text);
                query1.ViewFields = "<FieldRef Name='Dzien' /><FieldRef Name='Pracownik' />";
                SPListItemCollection elementyDoUsuniecia = witryna.Lists["Szczegoly"].GetItems(query1);

                for (int intIndex = elementyDoUsuniecia.Count - 1; intIndex > -1; intIndex--)
                {
                    if (Convert.ToDateTime(elementyDoUsuniecia[intIndex]["Dzien"]).ToShortDateString() == Convert.ToDateTime(TextBox4.Text).ToShortDateString() && elementyDoUsuniecia[intIndex]["Pracownik"].ToString() == DropDownList1.SelectedValue)
                    {
                        elementyDoUsuniecia.Delete(intIndex);
                    }
                }
            }

Getting a error: Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED)) Please help.

Why better to use:

            using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
            using (SPWeb witryna = siteCollection.OpenWeb())
Grzegorz Z
  • 1,709
  • 7
  • 42
  • 73

1 Answers1

6

Current Site
If you're going to work on the site where your code is running (could be web part on a page) then you should use SPContent.Current.Web and not to dispose it.

So don't do this:

var web = SPContext.Current.Web;
// some code
web.Dispose(); // DONT DO THIS

or

using (var web = SPContext.Current.Web) // DON'T DO THIS
{
  // some code
}

as this is the exact same as (just syntactical sugar implemented by the C# compiler)

try 
{
  var web = SPContext.Current.Web;
  // some code
}
finally
{
  web.Dispose(); // DONT DO THIS
}

Same Site Collection
If you're going to work on another site the the same site collection then you should use something like:

using (var web = SPContext.Current.Site.OpenWeb("URL_OF_SITE"))
{
  // Your code
}

I.e. get site from current SPSite object and remember to dispose it.

Other Site Collection
If you're going to work on a site which may not be in the same site collection then you should use something like:

using (var site = new SPSite("URL_OF_SITE"))
  using (var web = site.OpenWeb())
  {
    // Your code
  }

I.e. Get site collection and then get site from that remember to dispose both.

Per Jakobsen
  • 32,409
  • 1
  • 33
  • 62
  • The list in wchich I work should be Located on the site? Why I'm getting this error Attempted to use an object that has ceased to exist. (Exception from HRESULT: 0x80030102 (STG_E_REVERTED)) when i try to add to the site another web part's elements or list? – Grzegorz Z Nov 07 '12 at 10:00
  • If the frases using schould be before SPWeb witryna = SPContext.Current.Web, so ciode is:

    using(SPWeb witryna = SPContext.Current.Web) { // code }

    – Grzegorz Z Nov 07 '12 at 10:02
  • What does it mean "and not to dispose it." – Grzegorz Z Nov 07 '12 at 10:09
  • 1
    You shouldn't call SPWeb.Dispose on objects you create from SPContext. So do NOT do the following:

    SPWeb myweb = SPContext.Current.Web; myweb.Dispose();

    – Jussi Palo Nov 07 '12 at 10:13
  • 1
    See additions to answer – Per Jakobsen Nov 07 '12 at 10:31
  • So I schould never do web.Dispose() But If I done it in new procedure like Button Click I should opened again SPWeb witryna = SPContext.Current.Web – Grzegorz Z Nov 07 '12 at 10:38
  • Should I change SPWeb witryna = SPContext.Current.Web; to the var witryna = SPContext.Current.Web; all times when i call to SPContext ? – Grzegorz Z Nov 07 '12 at 10:48
  • So without Using, and without Dispose ? :) – Grzegorz Z Nov 07 '12 at 11:17
  • 1
    YES, neither Using nor Dispose. Whether you use SPWeb witryna = SPContext.Current.Web; or var witryna = SPContext.Current.Web; doesn't matter that's exactly the same. (I just prefer var when the type is obvious). Using SPWeb witryna = SPContext.Current.Web; isn't opening anything it's just making a local variable point to the already opened web, which is passed to all the controls on the page and therefore should be Dispose by your code. – Per Jakobsen Nov 07 '12 at 11:35