Reputation: 241
I am trying to bind my class using a Repeater Databind. However I get an error and not sure what I am doing wrong.
I should point out that the binding works when I use weatherinfo.list, but if I just use weatherinfo I get an error message:
An exception of type 'System.ArgumentException' occurred in System.Web.dll but was not handled in user code
Additional information: An invalid data source is being used for Repeater_weatherReports. A valid data source must implement either IListSource or IEnumerable.
My C# code is:
public partial class _Default : System.Web.UI.Page
{
string city_name;
string temp_min;
string temp_max;
string humidity;
string weatherdate;
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;
Label LblDate = e.Item.FindControl("DateWeather") as Label;
weatherdate = LblDate.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;//weatherinfo.list;
Repeater_weatherReports.DataBind();
foreach (List list in weatherinfo.list)
{
// if (list.main != null && list.main.city != null && list.main.city.name != null)
if (list.main != null && weatherinfo.city != null)
{
city_name = weatherinfo.city.name; //list.main.city.name;
weatherdate = list.main.weatherdate;
//lblDescription.Text = weatherinfo.list[0].weather[0].description;
temp_min = string.Format("{0}", Math.Round(list.main.temp_min, 1));
temp_max = string.Format("{0}", Math.Round(list.main.temp_max, 1));
humidity = list.main.humidity.ToString();
}
// tblWeather.Visible = true;
}
//lblCity_Country.Text = weatherinfo.city.name;
////lblDescription.Text = weatherinfo.list[0].weather[0].description;
//lblTempMin.Text = string.Format("{0}", Math.Round(weatherinfo.list[0].main.temp_min, 1));
//lblTempMax.Text = string.Format("{0}", Math.Round(weatherinfo.list[0].main.temp_max, 1));
//lblHumidity.Text = weatherinfo.list[0].main.humidity.ToString();
//tblWeather.Visible = true;
//SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\vagheld\Documents\Visual Studio 2015\WebSites\WeatherAPI\App_Data\Weatherdatabase.mdf;Integrated Security=True");
//con.Open();
//SqlCommand cmd = con.CreateCommand();
//cmd.CommandType = System.Data.CommandType.Text;
//cmd.CommandText = "INSERT INTO weather values ('"+lblCity_Country.Text+"', '"+lblTempMin.Text + "', '"+lblTempMax.Text + "', '"+DateTime.Now+"' )";
//cmd.ExecuteNonQuery();
//con.Close();
/*
lblCity_Country.Text = weatherinfo.city.name + "," + weatherinfo.city.country;
lblDescription.Text = weatherinfo.list[0].weather[0].description;
lblTempMin.Text = string.Format("{0}.c", Math.Round(weatherinfo.list[0].temp.min, 1));
lblTempMax.Text = string.Format("{0}.c", Math.Round(weatherinfo.list[0].temp.max, 1));
lblHumidity.Text = weatherinfo.list[0].humidity.ToString();
tblWeather.Visible = true;
*/
}
}
public class WeatherInfo
{
public string cod { get; set; }
public double message { get; set; }
public int cnt { get; set; }
public IList<List> list { get; set; }
public City city { get; set; }
}
public class Main
{
public double temp { get; set; }
public string weatherdate { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public int humidity { get; set; }
public double temp_kf { get; set; }
// public City city { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Sys
{
public string pod { get; set; }
}
public class List
{
public int dt { get; set; }
public Main main { get; set; }
public IList<Weather> weather { get; set; }
public Clouds clouds { get; set; }
public Wind wind { get; set; }
public Sys sys { get; set; }
public string dt_txt { get; set; }
}
public class Coord
{
public double lat { get; set; }
public double lon { get; set; }
}
public class City
{
public int id { get; set; }
public string name { get; set; }
public Coord coord { get; set; }
public string country { get; set; }
}
}
Part of my aspx 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("city") %>' />
<asp:Label runat="server" ID="DateWeather" Text='<%# Eval("main.weatherdate") %>' />
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>
I can't figure out why the binding works with 'weatherinfo.list' but not 'weatherinfo'. Any ideas?
Upvotes: 1
Views: 457
Reputation: 2172
This because WeatherInfo
class is itself not a collection. While WeatherInfo.list
is an implementation of IListSource. So while binding a datasource you need a collection object which must implement IListSource
or IEnumerable
interfaces.
Upvotes: 1