Search code examples
c#sql-serverdotnethighcharts

Serie from SQL Server


I am trying to store values from specific column from SQL Server Database to List<Serie>

I am using dotNet Highcharts.

Below is my code. It is having some issue.

using (SqlConnection cnn = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI"))
{
    SqlDataAdapter da = new SqlDataAdapter("select top(100) x from Table4 order by Id desc", cnn);
    DataSet ds = new DataSet();
    da.Fill(ds, "Table4");

    List<Serie> xValues = new List<Serie>();

    foreach (DataRow row in ds.Tables["Table4"].Rows)
    {
        xValues.Add(row["x"].ToString());
    }
}

Solution

  • The code to retrieve the data is perfectly fine. However, you are making the error when adding it to the list.

    You want to store data in a List<Serie>. Now we do not know what type of object Serie is, nor what properties it does have. But you try to add a string to your list.

    There are 2 ways of resolving this:

    Change the type of your list to List<String> xValues = new List<String>();

    or

    Change your xValues.Add() line to something which adds a valid object of type Serie.

    //you need to check Serie constructors to see which properties need to passed.
    xValues.Add(new Serie(row["x"]));