0

I am trying to get the labels in my .aspx file to work in my .cs file. I have created a method which I thought would bind the labelIDs into string variables, as using onitemcommand in my repeater.

Repeater code:

    <asp:Repeater ID="Repeater_weatherReports" runat="server" onitemcommand="reptrData_ItemCommand">
        <ItemTemplate>
            <table id="tblWeather" border="0" visible="true">
                <tr>
                    <th>
                        Weather Info
                    </th>
                </tr>
                <tr>
                    <td>
                        <asp:Label runat="server" ID="lblCity_Country" Text='<%# Eval("main.city") %>' />&nbsp;
                        humidity:<asp:Label runat="server" ID="Label_humidity" Text='<%# Eval("main.humidity") %>' />
                    </td>
                </tr>
                <tr>
                    <td>
                        min:<asp:Label runat="server" ID="Label_min" Text='<%# Eval("main.temp_min") %>' />
                        max:<asp:Label runat="server" ID="Label_max" Text='<%# Eval("main.temp_max") %>' />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:Repeater>

C# Code:

public partial class _Default : System.Web.UI.Page
{



     string city_name;
        string temp_min;
        string temp_max;
        string humidity;


        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected void reptrData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label lblCity = e.Item.FindControl("lblCity_Country") as Label;
            city_name = lblCity.Text;

            Label lblHumidity = e.Item.FindControl("Label_humidity") as Label;
            humidity = lblHumidity.Text;

            Label LblMin = e.Item.FindControl("Label_min") as Label;
            humidity = lblHumidity.Text;

            Label LblMax = e.Item.FindControl("Label_max") as Label;
            temp_max = lblHumidity.Text;


        }



        protected void GetWeatherInfo(object sender, EventArgs e)
        {


            string appID = "hidden";
            //string url = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&units=metric&cnt=2&APPID={1}",txtCity.Text,appID);

            string url = string.Format("http://api.openweathermap.org/data/2.5/forecast?q={0},us&units=metric&cnt=5&APPID={1}", txtCity.Text, appID);




            using (WebClient client = new WebClient())
            {
                string json = client.DownloadString(url);

                JavaScriptSerializer serializer = new JavaScriptSerializer();

                WeatherInfo weatherinfo = serializer.Deserialize<WeatherInfo>(json);

                Repeater_weatherReports.DataSource = weatherinfo.list;
                Repeater_weatherReports.DataBind();




                int i = 0;
                foreach (List list in weatherinfo.list)
                {





                    city_name = weatherinfo.list[i].main.city.name;
                    //lblDescription.Text = weatherinfo.list[0].weather[0].description;


                    temp_min = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_min, 1));
                    temp_max = string.Format("{0}", Math.Round(weatherinfo.list[i].main.temp_max, 1));
                    humidity = weatherinfo.list[i].main.humidity.ToString();
                   // tblWeather.Visible = true;



                    i++;
                }
    }

When my code runs, I get a NullReferenceException on the line

city_name = weatherinfo.list[i].main.city.name;

It seems like my reptrData_ItemCommand method is not being invoked from the .aspx file (from the OnItemCommand)

What am I doing wrong?

Thank you

1

2 Answers 2

2

As Koby explained, You can use a foreach element to get details

But in your case, you are getting null reference exception in below line

city_name = weatherinfo.list[i].main.city.name;

This error will also come because on the list at index 0 has either main is null or city is null or name is null

Use the below condition will null check

foreach (List list in weatherinfo.list)
{
    if (list.main != null && list.main.city != null && list.main.city.name != null ) 
    {
       city_name = list.main.city.name;
    }
    ....
}
Sign up to request clarification or add additional context in comments.

Comments

1

Youv'e got foreach and for loop usage mixed up. When you use foreach, you don't use the i iterator, you use the current list as an iterator:

foreach (List list in weatherinfo.list)
{
    city_name = list.main.city.name; 
    ....
}

And you don't have to use the i iterator. You can remove it from your code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.