2

需要帮助解决与 ASP.NET 应用程序数据缓存和图表控制相关的问题。我有一个包含图表控件的简单测试页。由于图表使用数据库中的数据,我创建了一个名为 chartData 的自定义对象,其中包含图表所需的所有数据。当我从头开始创建 chartData(因为它不在缓存中)时,图表会正确呈现。当我从缓存中检索 chartData 时,出现以下异常。我在调试器中检查了从缓存返回的对象,它看起来是正确的。关于这里发生的事情有什么建议吗?

这发生在我的开发系统(Win 7、VS 2010、C#)上。

因为它似乎与图表数据源有关,所以这是影响数据源属性的代码行:

myChart.DataSource = myChartData.DataSource;

myChartData.DataSource 是 myChartDataPoint 的列表,其中 myChartDataPoint 是

    public class myChartDataPoint
{
    public int X { get; set; }
    public int Y1 { get; set; }
    public int Y2 { get; set; }
    public int Y3 { get; set; }
}

“/”应用程序中的服务器错误。

你调用的对象是空的。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误:

在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪:

[NullReferenceException:对象引用未设置为对象的实例。] System.Web.UI.DataVisualization.Charting.ChartImage.DataBind(IEnumerable dataSource, ArrayList seriesList) +451 System.Web.UI.DataVisualization.Charting.Chart.PerformDataBinding (IEnumerable 数据)+14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable 数据)+128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调)+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(单分辨率) +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 适配器) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter 适配器)+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输出)+53 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,ControlAdapter 适配器)+27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer,ControlAdapter 适配器)+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) +3060HtmlControls.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(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3060HtmlControls.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(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+3060RenderChildren(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 适配器)+100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+3060RenderChildren(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 适配器)+100 System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+25 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+3060ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060


版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本: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; }
}

我得到了这个工作。也许有人可以告诉我为什么这会解决它,因为我不知道。我更换后异常消失了

foreach (Series series in chartData.SeriesList) myChart.Series.Add(series); 

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);
 } 

明显的区别是图表对象不再引用缓存的chartData.SeriesList。不知道为什么会有所作为。

4

0 回答 0