0

I'm new at developing an web part with Visual Studio. I want a weather web part. I created an empty SP 2013 Project, added Web Part item. But when I deploy it and add the custom webpart into my site. It gives an error:

Sorry, something went wrong An unexpected error has occurred.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.

I deleted the webpart and add again. Nothing changed. What else should I have to do? Thank you.

My WebPart item name : HavaDurumuWP My Project name: HavaDurumu

Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="HavaDurumuWP" List="113" Url="_catalogs/wp">
    <File Path="HavaDurumuWP\HavaDurumuWP.webpart" Url="HavaDurumuWP_HavaDurumuWP.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="Custom" />
    </File>
  </Module>
</Elements>

Here is my HavaDurumuWP.cs:

namespace HavaDurumuWP.HavaDurumuWP
{
    [ToolboxItemAttribute(false)]
    public class HavaDurumuWP : WebPart
    {

        public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                string res = Convert.ToString(e.Result);
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(res);
                var table = doc.DocumentNode.SelectSingleNode("//table[@class='tbl_sond']");
                var degree = table.SelectSingleNode("//td[@class='renkMax']");
                var date = table.SelectSingleNode("//td[@class='sond_zaman']");

            }
            LiteralControl filterMessage = new LiteralControl("ece");

            this.Controls.Add(filterMessage);

        }
        protected override void CreateChildControls()
        {
            string url = "http://www.mgm.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL";

            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            Uri uri = new Uri(url);
            webclient.DownloadStringAsync(uri);
        }
    }
}
2
  • do you have the permission to read the file that you get from e.Result ? Commented Apr 22, 2013 at 7:54
  • Where can I know the permissions?
    – e.ozmen
    Commented Apr 22, 2013 at 8:32

1 Answer 1

1

It seems like somewhere inside your webpart an exception is thrown. I would suggest you to wrap all your code inside CreateChildControls and client_DownloadStringCompleted methods with try..catch something like that:

try{
    ...
} catch (Exception ex){
    this.Controls.Add(new LiteralControl(ex.Message+"; "+ex.StackTrace));
}

It is a good practice to handle all possible exceptions inside webpart to disalow them to float up to the page (in the opposite case this exception can break the whole page).

Also you can try to debug your webpart (F5 in Visual Studio) to identify the place where exception is thrown.

2
  • When I add the content of the webpart says: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.; at System.Web.LegacyAspNetSynchronizationContext.OperationStarted() at System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken) at HavaDurumuWP.HavaDurumuWP.HavaDurumuWP.CreateChildControls()
    – e.ozmen
    Commented Apr 22, 2013 at 8:32
  • 1
    The problem is that you start asynchronous download in the CreateChildControls method. As asynchronous means that thread run further without waiting, the form is continuing rendering and shows to a client. Later, when you download weather data, it invokes client_DownloadStringCompleted, but form is already rendered and now it is impossible to change its view. So, try to use DownloadString instead DownloadStringAsync. If needs too much time, try to use JavaScript to get weather information on the client side.
    – MikhailSP
    Commented Apr 22, 2013 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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