【问题标题】:System.NotImplementedException in System.Core.dll when I use Chart Helper in ASP.NET当我在 ASP.NET 中使用图表助手时 System.Core.dll 中的 System.NotImplementedException
【发布时间】:2015-11-17 07:44:43
【问题描述】:

首先对不起我的英语。

我正在尝试从 W3School 学习 ASP.NET。关于如何在 Web 网格和图表助手中显示数据库中的数据有两个示例。

以下代码在网络网格中正常工作,并在网页中显示数据。

    @{

    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>

但是当我尝试将以下代码用于网络图表时,我收到了错误消息。

@{
    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();
} 

错误的截图是:

异常框指向这一行:"var myChart = new Chart(width: 600, height: 400)"

但是,当我使用此代码时,图表助手可以工作。

@{ 
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();
}

堆栈跟踪是:

在 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 操作) 在 System.Web.Helpers.Chart.GetBytes(字符串格式) 在 System.Web.Helpers.Chart.Write(字符串格式) 在 c:\Users\Roham\Documents\Visual Studio 2015\Projects\WebPagesLab\WebPagesLab\Products.cshtml 中的 ASP._Page_Products_cshtml.Execute():第 10 行 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 在 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 执行器) 在 System.Web.WebPages.WebPage.ExecutePageHierarchy() 在 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage) 在 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext)

很高兴听到您的建议。

【问题讨论】:

  • 请将异常堆栈跟踪添加到您的帖子中。
  • 添加了堆栈跟踪和表定义的屏幕截图。感谢您的关注。
  • DB.query的返回类型是什么?
  • 是的。该问题的答案并未完全描述问题。问题是,当您给 BindDataTable 方法一个 IEnumerable 时,它​​在内部调用 IEnumerable.Reset() 并且此实现没有该方法的实现并抛出此异常。

标签: c# asp.net asp.net-mvc charts


【解决方案1】:

在您的情况下解决它的最简单方法是在生成Chart 时获取查询结果并将它们作为数组或其他具体的可枚举数据结构传递。试试这个:

@{
    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();
}

类似问题:Can't bind datatable to Chart Control

这里提到DataTable没有实现IEnumerable,所以不能直接处理数据库查询结果。 Chart.AddSeries() 也有同样的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2011-06-08
    • 2018-08-27
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多