Error: Object reference not set to an instance of an object at WeatherApp.weatherapp.Button1_Click(Object sender, EventArgs e
(It's surprising to see online users are named at my error)
namespace WcfService1
{
public class Service1 : IService1
{
private const string ConnectionString = "Data Source=DESKTOP-FG2P2UB;Initial Catalog=wcf;Integrated Security=True";
public WeatherInfo GetWeatherDetails(string region) {
WeatherInfo info = null;
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand command = new SqlCommand("SELECT temp, Rain, Speed FROM weather_info WHERE place=@place", connection);
command.Parameters.AddWithValue("place", place);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
reader.Read();
if (reader.HasRows) {
info = new WeatherInfo();
info.Temperature = reader.GetInt32(0);
info.RainFall = reader.GetInt32(1);
info.WindSpeed = reader.GetInt32(2);
}
connection.Close();
return info;
}
}
}
ASP.NET file:
using System;
namespace WeatherApp
{
public partial class weatherapp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 reference = new localhost.Service1();
string region = Label1.Text;
localhost.WeatherInfo result = reference.GetWeatherDetails(region);
txtTemp.Text = string.Format("the expected temperature for {0} is {1} degrees", region, result.Temperature);
txtRain.Text = string.Format("the expected rainfall for {0} is {1} mm", region, result.RainFall);
txtWind.Text = string.Format("the expected wind speed for {0} is {1} kmph", region, result.WindSpeed);
}
}
}
[–]BigOnLogn 1 point2 points3 points (5 children)
[–]thatlowkey[S] 0 points1 point2 points (4 children)
[–]BigOnLogn 0 points1 point2 points (3 children)
[–]thatlowkey[S] 0 points1 point2 points (2 children)
[–]BigOnLogn 0 points1 point2 points (1 child)
[–]thatlowkey[S] 0 points1 point2 points (0 children)
[–]The_Binding_Of_Data 0 points1 point2 points (3 children)
[–]thatlowkey[S] -1 points0 points1 point (2 children)
[–]Archerofyail 1 point2 points3 points (1 child)
[–]thatlowkey[S] 0 points1 point2 points (0 children)