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())