2017-06-07 9 views
1

私はvs2012 asp.net mvcとcrystal reports v.13を使用しています。データセットはCrystalレポートでヌル入力可能なシステムをサポートしていません

i>は

DataSetが可能System.Nullable <をサポートしていない結晶レポートをエクスポートしようとしているとき、私はこのエラーを持っています。

この行で

rd.SetDataSource(cn.Customers.Select(C =>新しい

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>

答えて

2

"cn.Customers.Select"は、その属性が推論型を持つ匿名型を返していることを理解しています。

"rd.SetDataSource"は、その匿名型のオブジェクトを取得し、ReportDocumentにDataSetを作成します。

匿名型が(Customerクラスの属性のために)null可能な推論型を持つと、フレームワークはnullable型を使用してDataColumnを作成しようとします。だからあなたはエラーが言及される。

したがって、匿名型を駆動して、その属性のnull可能な型を避けることができます。これを行うには、以下を試してください。しかし、私はあなたの顧客クラスを知らないので、私はシナリオを簡素化し、それが1つだけのプロパティを持っていると仮定し、それはnullableです。

class Customer { public int? CustomerID; } 

その後、あなたはこれを試みることができる:

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

はあなたのNULL可能な属性のすべてのためにそのような何かを行います。

0

、私は、解決策を見つけた生成モデルクラスに、あなたのクラスのcostumersがDatetimeと同様、複数の可能System.Nullableフィールドを持っていることがわかります、ダブルス...

あなたのすべての定義を変更する必要がありますこれはあなたの問題を解決する通常の

Nullable<DateTime> date_exemple; 

希望

DateTime date_exemple; 

にまで可能System.Nullableからフィールドと幸せは

0をコーディング
関連する問題