views:

58

answers:

2

webpart is working on local server but not working on production server when "SPSite" class is uesd in the web part. On the production server it throws error. If I do not use that class in the webpart the web part also works on live machine.

Any idea what might be causing the error?

the code in the webpart is this:

namespace CompanyNews
{
    [Guid("40de3c60-9e30-4050-b9f3-01e71868f522")]
    public class CompanyNews : System.Web.UI.WebControls.WebParts.WebPart
    {
        private HtmlTextWriter writer;
        public CompanyNews()
        {
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            using (SPSite site = SPContext.Current.Site)
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string listName = "News Display";
                    writer.Write(listName);
                    SPList list = null;
                    foreach (SPList currentList in web.Lists)
                    {
                        if (currentList.Title.Equals(listName,
                                                     StringComparison.InvariantCultureIgnoreCase))
                        {
                            list = currentList;
                            break;
                        }
                    }
                    writer.WriteBeginTag("ul");
                    foreach (SPListItem item in list.Items)
                    {                    
                        writer.Write("<li style=\"font-size:12px;padding:1px\">");
                        writer.Write(item["Title"].ToString() + "...   ");
                        writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>");
                        writer.Write("</li>");
                    }
                    writer.WriteEndTag("ul");
                }
            }
        }

    }
}

The dll of the webpart is in the bin folder and in the web.config file there is an entry for the web par as a safe control. Other webpart which displays a "hellow world" message is also uploaded to production the same way. I i guess its the code that is causing the problem.

The error message is: "An error occurred while previewing the web part"

+1  A: 

Hi - just something I noticed, you shouldn't wrap objects from the Current Context in a using statement. Good article here Clicky

Better practice would be to use the following

using (SPSite mySite = new SPSite(SPContext.Current.Site.Url))
{
...
} 

Also you should look at packaging up your solution in a WSP, allowing stsadm to deploy it. Dragging into the GAC isn't very good practice.

Shane

Shaneo
A: 

The SPSite object isn't getting reference anywhere that I can see. Why don't you remove it anyway as it's superflous to your needs?

SPWeb web = SPContext.Current.Web;  
                string listName = "News Display"; 
                writer.Write(listName); 
                SPList list = null; 
                foreach (SPList currentList in web.Lists) 
                { 
                    if (currentList.Title.Equals(listName, 
                                                 StringComparison.InvariantCultureIgnoreCase)) 
                    { 
                        list = currentList; 
                        break; 
                    } 
                } 
                writer.WriteBeginTag("ul"); 
                foreach (SPListItem item in list.Items) 
                {                     
                    writer.Write("<li style=\"font-size:12px;padding:1px\">"); 
                    writer.Write(item["Title"].ToString() + "...   "); 
                    writer.Write("<a class=\"smallerred\" href=\"#\">Read More</a>"); 
                    writer.Write("</li>"); 
                } 
                writer.WriteEndTag("ul"); 
sparkymark75