0

I am developing a Chart Control using ASP.NET C# and I am facing this error:

System.IndexOutOfRangeException: 'USED'

My question is, because USED is my newly derived column, how can I improvise my codes to accept it?

The chart type I am using is PIE CHART.

string cs = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conStr))
{
    SqlCommand cmd = new SqlCommand("SELECT  SUM(USED)FROM(SELECT COUNT(ADMIN_NO) * Courses.PricePerPax AS USED FROM Student_Prog_Course INNER JOIN COURSES ON COURSES.CID = Student_Prog_Course.Course_ID GROUP BY COURSES.PricePerPax) u", con);

    Series series = Chart1.Series["Series1"];
    //Series series1 = Chart1.Series["Series2"];
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        series.Points.AddY(rdr["USED"].ToString());
    }
}

Result

(No column name)
6845.46

1 Answers1

1

As @tontonsevilla pointed out in comment section, you can set alias for SUM(USED)

SqlCommand cmd = new SqlCommand("SELECT SUM(USED) AS USED FROM(SELECT COUNT(ADMIN_NO) * Courses.PricePerPax AS USED FROM Student_Prog_Course INNER JOIN COURSES ON COURSES.CID = Student_Prog_Course.Course_ID GROUP BY COURSES.PricePerPax) u", con);

or second option you have is that getting first column value as follows:

series.Points.AddY(rdr[0].ToString());

See: Read data from SqlDataReader

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28