Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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())
share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
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 7 '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 7 '12 at 10:02
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 7 '12 at 10:13
1  
See additions to answer –  Per Jakobsen Nov 7 '12 at 10:31
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 7 '12 at 11:35
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.