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

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()) +
                    "<BR>");
            }
        }

    }
share|improve this question
    
Can you post the rest of the code? –  Mike Oryszak Sep 27 '10 at 2:50
add comment

2 Answers

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

http://blogs.msdn.com/b/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx

share|improve this answer
add comment

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
add comment

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.