Reputation: 1209
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());
}
}
Upvotes: 0
Views: 94
Reputation: 23937
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"]));
Upvotes: 1
Reputation: 2045
I think you to open connection before accessing DB data and closed after access data.
using (SqlConnection cnn = new SqlConnection("Data Source=INBDQ2WK2LBCD2S\\SQLEXPRESS;Initial Catalog=MCAS;Integrated Security=SSPI"))
{
cnn.Open();
SqlDataAdapter da = new SqlDataAdapter("select top(100) x from Table4 order by Id desc", cnn);
DataSet ds = new DataSet();
da.Fill(ds, "Table4");
cnn.Close();
List<String> xValues = new List<String>();
foreach (DataRow row in ds.Tables["Table4"].Rows)
{
xValues.Add(row["x"].ToString());
}
}
Upvotes: 0