Need help resolving a problem related to ASP.NET application data caching and chart control. I have a simple test page containg the chart control. Since the chart uses data from the database, I have created a custom object called chartData that contains all data needed by the chart. When I create the chartData from scratch (because it's not in cache), the chart renders correctly. When I retrieve the chartData from the cache, I get the exception below. I inspected the object returned from the cache in the debugger, and it appears correct. Any suggestions on what's going on here?
This is happening on my development system (Win 7, VS 2010, C#).
As it seems to be relate to the chart datasource, here is the line of my code that affect the datasource property:
myChart.DataSource = myChartData.DataSource;
myChartData.DataSource is a List of myChartDataPoint where myChartDataPoint is
public class myChartDataPoint
{
public int X { get; set; }
public int Y1 { get; set; }
public int Y2 { get; set; }
public int Y3 { get; set; }
}
Server Error in '/' Application.
Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.DataVisualization.Charting.ChartImage.DataBind(IEnumerable dataSource, ArrayList seriesList) +451 System.Web.UI.DataVisualization.Charting.Chart.PerformDataBinding(IEnumerable data) +14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 System.Web.UI.DataVisualization.Charting.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly) +400 System.Web.UI.DataVisualization.Charting.ChartImage.GetImage(Single resolution) +1035 System.Web.UI.DataVisualization.Charting.Chart.SaveImage(Stream imageStream) +124 System.Web.UI.DataVisualization.Charting.Chart.Render(HtmlTextWriter writer) +375 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208 System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +173 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +53 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8 System.Web.UI.Page.Render(HtmlTextWriter writer) +29 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
/// <summary>
/// Spline Chart Data
/// </summary>
public class ChartData
{
private SeriesList _seriesList = new SeriesList();
private ChartDataPoint3YList _dataSource = new ChartDataPoint3YList();
private StringList _legendTextList = new StringList();
private StringList _axisXLabelList = new StringList();
/// <summary>
/// Chart Title
/// </summary>
public string ChartTitle { get; set; }
/// <summary>
/// X axis title
/// </summary>
public string AxisXTitle { get; set; }
/// <summary>
/// Y axis title
/// </summary>
public string AxisYTitle { get; set; }
/// <summary>
/// X Axis point label strings.
/// </summary>
public StringList AxisXLabelList
{
get { return _axisXLabelList; }
set { _axisXLabelList = value; }
}
/// <summary>
/// Legend strings.
/// </summary>
public StringList LegendTextList
{
get { return _legendTextList; }
set { _legendTextList = value; }
}
/// <summary>
/// Datasource to be used by the chart, contains data for all chart data points
/// </summary>
public ChartDataPoint3YList DataSource
{
get { return _dataSource; }
set { _dataSource = value; }
}
/// <summary>
/// Chart series collection
/// </summary>
public SeriesList SeriesList
{
get { return _seriesList; }
set { _seriesList = value; }
}
/// <summary>
/// Chart data specific to one chart series
/// </summary>
public class ChartSeriesData
{
/// <summary>
/// Legend text for the series
/// </summary>
public string Legend { get; set; }
/// <summary>
/// Data Points of the series
/// </summary>
public ChartDataPoint3YList DataPoints { get; set; }
}
}
public class ChartDataPoint3YList : List<ChartDataPoint3Y> { }
public class ChartDataPoint3Y
{
public int X { get; set; }
public int Y1 { get; set; }
public int Y2 { get; set; }
public int Y3 { get; set; }
}
I got this to work. Perhaps someone could tell me why this fixed it because I don’t know. The exception went away after I replaced
foreach (Series series in chartData.SeriesList) myChart.Series.Add(series);
with
foreach (Series series in chartData.SeriesList)
{
SeriesItem newItem = new Series();
newItem … set the item properties using the relevant values from series
…
myChart.Series.Add(newItem);
}
The obvious difference is that the chart object no longer references cached chartData.SeriesList. Have no idea why that made a difference.