首页 > 解决方案 > 尝试在 SQL 中从我的数据库中选择数据时引发 System.IndexOutOfRangeException。ASP.NET

问题描述

我正在尝试从图表中选择并显示我的应用程序中的平均温度。执行我的 SQL 命令后,我希望在图表中显示平均温度,但它会抛出:

System.IndexOutOfRangeException:'温度'

这是我拥有的代码。如果有人可以帮助我,我将不胜感激。

    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();

    }

标签: c#asp.netsql-serverdatabase

解决方案


推荐阅读