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:
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.Iterator
1.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(Action
1 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)
I'd be appreciated to hear your advises.