簡體   English   中英

數據集不支持Crystal報表中的系統可為空

[英]dataset does not support system nullable in crystal reports

我正在使用vs2012 asp.net mvc和Crystal Reports v.13。

當我嘗試導出水晶報表時出現此錯誤

DataSet不支持System.Nullable <>。

在這條線

rd.SetDataSource(cn.Customers.Select(c => new

    public ActionResult Index()
    {
        ViewBag.listCustomers = cn.Customers.ToList();
        return View();
    }
    public ActionResult Export()
    {
        ReportDocument rd = new ReportDocument();
        rd.Load(Path.Combine(Server.MapPath("~/Reports/CustomerReport.rpt")));
        rd.SetDataSource(cn.Customers.Select(c => new
        {
            CustomerID = c.CustomerID  ,
            CustomerName = c.CustomerName ,
            CustomerEmail = c.CustomerEmail ,
            CustomerZipCode = c.CustomerZipCode ,
            CustomerCountry = c.CustomerCountry ,
            CustomerCity = c.CustomerCity,
        }).ToList());

        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();
        Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
        stream.Seek(0, SeekOrigin.Begin);

        return File(stream, "application/pdf", "CustomerList.pdf");
  }

這是視圖:

 <table> <tr> <td>CustomerID</td> <td>CustomerName</td> <td>CustomerEmail</td> <td>CustomerZipCode</td> <td>CustomerCountry</td> <td>CustomerCity</td> </tr> @foreach (var cust in ViewBag.listCustomers) { <tr> <td>@cust.CustomerID</td> <td>@cust.CustomerName</td> <td>@cust.CustomerEmail</td> <td>@cust.CustomerZipCode</td> <td>@cust.CustomerCountry</td> <td>@cust.CustomerCity</td> </tr> } <br /> <br /> <a href="@Url.Action("Export","Customer")">Export report</a> </table> 

我了解到“ cn.Customers.Select”將返回其屬性具有推斷類型的匿名類型。

然后,“ rd.SetDataSource”將使用該匿名類型的對象,並為ReportDocument創建一個DataSet。

一旦匿名類型具有可為空的推斷類型(由於Customer類的屬性),框架便嘗試使用該可為空的類型創建DataColumn。 因此,您得到提到的錯誤。

因此,您可以嘗試驅動匿名類型,以避免其屬性為可空類型。 為此,請嘗試以下操作。 但是由於我不知道您的Customer類,因此我將簡化方案並假定它只有一個屬性並且它可以為空。

class Customer { public int? CustomerID; }

然后,您可以嘗試以下操作:

rd.SetDataSource(cn.Customers.Select(c => new
{
    CustomerID = c.CustomerID == null ? 0 : c.CustomerID
}).ToList());

對所有可為空的屬性執行類似的操作。

我找到了解決方案,在generate模型類中,您會發現您的Costumers類具有多個system.nullable字段,例如Datetimes,doubles,...

您需要將字段的所有定義從system.nullable更改為normal

Nullable<DateTime> date_exemple; 

DateTime date_exemple;

希望這能解決您的問題並祝您編程愉快

可空字段必須寫有值。 例如如下所示。

rd.SetDataSource(cn.Customers.Select(c => new
{
    CustomerID = c.CustomerID.Value
}).ToList());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM