0

First sorry for my English.

I'm trying to learn ASP.NET from W3School. There are two examples about how to show our data from a database in a web grid and chart helper.

The code below works properly in web grid and show the data in a web page.

    @{

    var db = Database.Open("SmallBekery");
    var selectQueryString = "SELECT * FROM Product ORDER BY Name";
    var data = db.Query(selectQueryString);
    var grid = new WebGrid(data);    
}

<div id="grid">
    @grid.GetHtml()
</div>

but when I try to use below code for web chart, I am getting error message.

@{
    var db = Database.Open("SmallBekery");
    var dbdata = db.Query("SELECT Name, Price FROM Product");
    var myChart = new Chart(width: 600, height: 400)
       .AddTitle("Product Sales")
       .DataBindTable(dataSource: dbdata, xField: "Name")
       .Write();
} 

And the screenshot of the error is: Error message screenshot

The exception box point to this line: "var myChart = new Chart(width: 600, height: 400)"

However, when I use this code, the chart helper works.

@{ 
var myChart = new Chart(width: 600, height: 400) 
   .AddTitle("Employees") 
   .AddSeries(chartType: "column",
      xValue: new[] {  "Peter", "Andrew", "Julie", "Mary", "Dave" }, 
      yValues: new[] { "2", "6", "4", "5", "3" }) 
   .Write();
}

The stack trace is:

at System.Linq.Enumerable.Iterator1.System.Collections.IEnumerator.Reset() at System.Web.UI.DataVisualization.Charting.ChartImage.GetDataSourceMemberNames(Object dataSource, Boolean usedForYValue) at System.Web.UI.DataVisualization.Charting.ChartImage.DataBindTable(IEnumerable dataSource, String xField) at System.Web.Helpers.Chart.DataBindChart(Chart chart) at System.Web.Helpers.Chart.ExecuteChartAction(Action1 action) at System.Web.Helpers.Chart.GetBytes(String format) at System.Web.Helpers.Chart.Write(String format) at ASP._Page_Products_cshtml.Execute() in c:\Users\Roham\Documents\Visual Studio 2015\Projects\WebPagesLab\WebPagesLab\Products.cshtml:line 10 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) at System.Web.WebPages.WebPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)

Table Definition

I'd be appreciated to hear your advises.

Parham.D
  • 1,158
  • 2
  • 12
  • 31
  • Please add the exception stack trace to your post. – Oguz Ozgul Nov 17 '15 at 07:50
  • Added stack trace and a screenshot of table definition. Thank you for your attention. – Parham.D Nov 18 '15 at 04:39
  • Same error here http://stackoverflow.com/questions/12516519/binding-datareader-to-asp-chart-control – Oguz Ozgul Nov 18 '15 at 05:45
  • What is the return type of DB.query? – Oguz Ozgul Nov 18 '15 at 05:52
  • I used GetType() and the result was: " System.Linq.Enumerable+WhereSelectListIterator`2[System.Collections.Generic.Dictionary`2[System.String,System.Object],WebMatrix.Data.DynamicRecord]" – Parham.D Nov 18 '15 at 07:27
  • When I try convert db.query to a nonsense type (to just get an error message) the error message show db.query type as a "System.Collections.Generic.IEnumerable" and I am getting the same error when I use this type instead the var one. Thanks. – Parham.D Nov 18 '15 at 07:33
  • 1
    Yes. The answer in that question is not completely describing th problem. The problem is, when you give the BindDataTable method an IEnumerable, it internally calls IEnumerable.Reset() and this implementation does not have an implementation for that method and throws this exception. – Oguz Ozgul Nov 18 '15 at 07:38

1 Answers1

0

The simplest way to solve it in your situation would be to get the query results and pass them as an array or another concrete enumerable data structure when generating the Chart. Try this:

@{
    var db = Database.Open("SmallBekery");
    var dbdata = db.Query("SELECT Name, Price FROM Product");
    var myChart = new Chart(width: 600, height: 400)
       .AddTitle("Product Sales")
       .DataBindTable(dataSource: dbdata.ToArray(), xField: "Name")
       .Write();
}

Similar question: Can't bind datatable to Chart Control

As mentioned here, DataTable does not implement IEnumerable, so it cannot deal with the database query result directly. Chart.AddSeries() suffers from the same problem.

Community
  • 1
  • 1
Demat
  • 39
  • 2
  • 12