Weather Webpart by using Yahoo Service in Share Point

Leave a Comment
Hello folks,

In a following post we will see how to build Weather Report  by using Yahoo Web service.
We need to pass city codes as a Parameter.

Please find the below code snippet to build Weather Webpart. below code is self explanatory.


   DataTable dtWeather = new DataTable();
   // Adding columns to data table
   dtWeather.Columns.Add("ImgSrc");
   dtWeather.Columns.Add("Place");
   dtWeather.Columns.Add("temp");
   try
   {
      // url to get the weather from yahoo web service
    if (!string.IsNullOrEmpty(strCityCodes))
     {
      url = "http://weather.yahooapis.com/forecastrss?p=" + strCityCode;
                             
      DataSet ds = new DataSet();
      XmlDocument xmlDoc = new XmlDocument();
      HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url);
      HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();
      try
        {
            // Check when no internet
         if (wResponse.StatusCode == HttpStatusCode.OK)
          {
           StreamReader StreamHandler = new                              System.IO.StreamReader(wResponse.GetResponseStream());
           xmlDoc.LoadXml(StreamHandler.ReadToEnd());
           XmlNodeReader data = new XmlNodeReader(xmlDoc);
           ds.ReadXml(data);

         ImageURL1 = ds.Tables["item"].Rows[0]       ["description"].ToString().Substring(11).Split('"');

      if(string.IsNullOrEmpty(Convert.ToString(ImageURL1)))
      {
      }
     else
     {
      Weather = ds.Tables["condition"].Rows[0]["temp"].ToString();
     }
    if (string.IsNullOrEmpty(Weather))
   {
       WeatherAni.Visible = false;
       WeatherWebPart.Visible = false;
       lblError.Visible = true;
       lblError.Text = "There is No Data for the Weather from Yahoo Web Service";
    }
   else
    {
     Double temp = Convert.ToDouble(Weather);
     int ctemp = (int)((temp - 32) * 5 / 9);
     String Weather1 = string.Format("{0}° C", ctemp);
     DataRow dtlistRow = dtWeather.NewRow();
     dtlistRow["ImgSrc"] = Convert.ToString(ImageURL1[0]);

      if (!string.IsNullOrEmpty(strDisplayName[code]))
      {
       dtlistRow["Place"] = Convert.ToString(strDisplayName[code]);
      }
      else
      {
       dtlistRow["Place"] = string.Empty;
      }
     dtlistRow["temp"] = Convert.ToString(Weather1);
    dtWeather.Rows.Add(dtlistRow);
  }
  }
 }
 catch (Exception ex)
 {
 //Exception Handling
 }
 }

 if (dtWeather.Rows.Count > 0)
 {
  reptWeather.DataSource = dtWeather;
  reptWeather.DataBind();
  }
  else
  {
     //
  }
  
}

Thanks.

Related Post

0 comments:

Post a Comment