SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can somebody please tell me what is wrong with this code?

protected void Button1_Click(object sender, EventArgs e) { using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb) {

            SPList oList = oWebsiteRoot.Lists["Tasks"];

            SPQuery oQuery = new SPQuery();
            oQuery.Query = "<Where><Eq><FieldRef Name='Status'/>" +
                "<Value Type='Text'>Completed</Value></Eq></Where>";
            SPListItemCollection collListItems = oList.GetItems(oQuery);

            foreach (SPListItem oListItem in collListItems)
                Response.Write(SPEncode.HtmlEncode(oListItem["Title"].ToString()) +

share|improve this question
Can you post the rest of the code? – Mike Oryszak Sep 27 '10 at 2:50
up vote 3 down vote accepted

Basically you should not dispose the Rootweb, if you are using the SPContext. Microsoft has updated the best practices

here is one example mentioning

share|improve this answer

Are u disposing any SPWeb or SPSite object that you intantiated using the SPContext object? If so then you need not to dispose any of them.

share|improve this answer

Your Answer


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.