I am trying to select and display the average temperature in my application from a chart. After the execution of my SQL command I expected to show the average temperature in my chart but it throws :
System.IndexOutOfRangeException:'Temperature'
This is the code that I have . I will be grateful if someone can help me .
private void Database(string location, string temperature, string unit)
{
SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\MyPC\source\repos\NHLweatherStation\NHLweatherStation\Database1.mdf;Integrated Security=True");
connection.Open();
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Weather (location,Temperature,date,unitsTemperature) VALUES ('" + location + "','" + temperature + "','" + DateTime.Now + "','" + unit + "')", connection);
sqlCommand.ExecuteNonQuery();
connection.Close();
SqlCommand cmd = new SqlCommand("Select avg(Temperature),date From Weather Group by date", connection);
connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();
TrendGraph.Series["Series1"].XValueMember = "date";
TrendGraph.Series["Series1"].YValueMembers = "Temperature";
TrendGraph.DataSource = rdr;
TrendGraph.DataBind();
connection.Close();
}