What I am trying to do is generate the chart using a SQL Server Database View. This view returns two columns as a top 5, Name and Count. This is my code.
When I try filling the data, it blows up on me.
- Code: Select all
Connection string:
SqlConnection sqlconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
- Code: Select all
string sqlQueryTop5High = "SELECT [Name], [Count] FROM v_Highs;";
- Code: Select all
SqlCommand sqlcmdTop5High = new SqlCommand(sqlQueryTop5High, sqlconn);
using (SqlDataAdapter da3 = new SqlDataAdapter(sqlcmdTop5High))
try
{
DataTable dt3 = new DataTable();
da3.Fill(dt3); // This is where it dies.
InitChartData(chart3, dt3);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.ToString());
}
finally
{
sqlconn.Close();
}
And this is the code that is supposed to generate the chart data
- Code: Select all
#region InitChartData Helper
private static void InitChartData(IChart chart1, DataTable dt1)
{
//set series name
ChartSeriesFormatCollection ctf = chart1.Series;
for (int c = 0; c < dt1.Columns.Count; c++)
{
chart1.ChartData[0, c].Text = dt1.Columns[c].Caption;
if (c > 0)
{
ctf.Append(chart1.ChartData[0, c]);
}
}
ChartCategoryCollection catg = chart1.Categories;
for (int r = 0; r < dt1.Rows.Count; r++)
{
//set category
catg.Append(chart1.ChartData[r + 1, 0]);
//set chartData value
object[] data = dt1.Rows[r].ItemArray;
for (int c = 0; c < data.Length; c++)
{
chart1.ChartData[r + 1, c].Value = data[c];
}
//set series value
for (int i = 0; i < ctf.Count; i++)
{
chart1.Series[i].Values.Add(chart1.ChartData[r + 1, i + 1]);
}
}
for (int i = 0; i < chart1.Series.Count; i++)
{
ChartSeriesDataFormat series = chart1.Series[i];
for (int j = 0; j < series.Values.Count; j++)
{
ChartDataLabel label = series.DataLabels.Add();
label.ID = j;
label.LabelValueVisible = true;
label.TextProperties.Paragraphs[0].DefaultCharacterProperties.FontHeight = 10;
label.TextProperties.Paragraphs[0].DefaultCharacterProperties.LatinFont = new TextFont("Arial");
}
}
}
#endregion
What am I doing wrong???