Question

I am making an application which has quite heavy (in sense of data) charts to be rendered when the ASP.NET page is loaded, due to this reason the page is taking much time to load and a white screen is shown. I am using Dundas chart API for my charts.

As a workaround I am trying to load the charts within Ajax calls. In this approach I render whole of page at once and once it is rendered, I send an Ajax call to server for chart control.

To render chart control I am using following code

private string GetSummaryGraph()
    {
        Chart chart_analysis = new Chart();
        chart_analysis.ID = "chart_analysis_1";
        chart_analysis.BackColor = Color.Black;
        chart_analysis.EnableViewState = true;
        chart_analysis.Height = 217;
        chart_analysis.ImageStorageMode = ImageStorageMode.UseImageUrl;
        chart_analysis.ImageUrl = "~/MyApp/ChartImages/MyChart_#SEQ(1000,720)";
        chart_analysis.ImageType = ChartImageType.Png;
        chart_analysis.Palette = ChartColorPalette.Dundas;
        chart_analysis.ViewStateContent = SerializationContent.All;
        chart_analysis.Width = 996;

        // Set all styling here, like ChartAreas, Series.

        string chartImgURL = chart_analysis.GetCurrentImageUrl(); // this line throws exception "Object reference not set to instance of object"

        string html_chart = "";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        chart_analysis.RenderControl(htw);

        html_chart = sw.ToString();
        return html_chart;
}

for some reason when I call the function chart_analysis.GetCurrentImageUrl(); it is throwing exception with following stacktrace

Object reference not set to an instance of an object.
at Dundas.Charting.WebControl.Chart.a(String )
   at Dundas.Charting.WebControl.Chart.GetCurrentImageUrl()
   at GetSummary.GetSummaryGraph() in D:\Projects\MyApp\GetSummary.aspx.cs:line 4492

However if I place a chart control in .aspx page rather than .aspx.cs file, this function works fine.

Edit

I have also tried to render control before calling GetCurrentImageURL(), and I get the following exception in this case as well.

Object reference not set to an instance of an object.
   at Dundas.Charting.WebControl.Chart.a(String )
   at Dundas.Charting.WebControl.Chart.GetCurrentImageUrl()
   at Dundas.Charting.WebControl.Chart.Render(HtmlTextWriter output)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at GetSummary.GetSummaryGraph() in D:\Projects\MyApp\GetSummary.aspx:line 4494
   at GetSummary.LoadChartData() in D:\Projects\MyApp\GetSummary.aspx:line 4378

It looks like I am missing some initialization which is automatically done when chart is placed in .aspx file. but I am unable to find what is this exactly?

Was it helpful?

Solution

You get this error because at that time you ask for it the chart control have not been run yet.

Also you run your chart control into a string and not direct on the page.

Consider two thinks.

Ether try to get the current image url after you render the control, ether you simple do not need it because the control automatically runs into memory and you get the html that you have to place on a point on your page.

In any case you need to run the control steps to make available the parameters of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top