DataSet不支持导出中的System.Nullable <>

我正在尝试使用导出为Excell,PDF,TextFile生成报告。 那么我在MVC做这个。 我有一个名为SPBatch的类(这是我的SQL中的存储过程的确切名称),它包含以下内容:

public string BatchNo { get; set; } public string ProviderName { get; set; } public Nullable<System.Int32> NoOfClaims { get; set; } public Nullable<System.Int32> TotalNoOfClaims { get; set; } public Nullable<System.Decimal> TotalBilled { get; set; } public Nullable<System.Decimal> TotalInputtedBill { get; set; } public Nullable<System.DateTime> DateCreated { get; set; } public Nullable<System.DateTime> DateSubmitted { get; set; } public Nullable<System.DateTime> DueDate { get; set; } public string Status { get; set; } public string RefNo { get; set; } public string BatchStatus { get; set; } public string ClaimType { get; set; } 

正如你可以看到我的一些列被声明为可空。 它从查找和显示结果顺利进行。 我有几个button下面是导出的图像button,每次我尝试在Excel中导出,我总是得到的问题“数据集不支持System.Nullable <> ”在我的代码的这一部分:

 foreach (MemberInfo mi in miArray) { if (mi.MemberType == MemberTypes.Property) { PropertyInfo pi = mi as PropertyInfo; dt.Columns.Add(pi.Name, pi.PropertyType); //where the error pop's up. } else if (mi.MemberType == MemberTypes.Field) { FieldInfo fi = mi as FieldInfo; dt.Columns.Add(fi.Name, fi.FieldType); } } 

该错误显示在一个评论。 你能帮我做什么吗? 我尝试添加DBNull在我的代码,但仍然得到相同的错误。 我试图删除我的SPBatch Nullable,但我得到一个错误,有些表需要声明为可空。

我该怎么办?

尝试

 dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType( pi.PropertyType) ?? pi.PropertyType); 

感谢一个C#版本的生成一个数据表和一些黑客,我可以在VB中提供这个答案 – 我把它放在这里,因为我刚刚有很多麻烦,希望从一个存储过程获得一个可过滤的数据集,同时使用一个简单的数据层。 我希望它能帮助别人!

注意:用例是你希望使用BindingSource.Filter =“某个查询string”的地方:

 Imports System.Reflection Public Module Extenders <System.Runtime.CompilerServices.Extension> Public Function ToDataTable(Of T)(collection As IEnumerable(Of T), tableName As String) As DataTable Dim tbl As DataTable = ToDataTable(collection) tbl.TableName = tableName Return tbl End Function <System.Runtime.CompilerServices.Extension> Public Function ToDataTable(Of T)(collection As IEnumerable(Of T)) As DataTable Dim dt As New DataTable() Dim tt As Type = GetType(T) Dim pia As PropertyInfo() = tt.GetProperties() 'Create the columns in the DataTable For Each pi As PropertyInfo In pia Dim a = If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType) dt.Columns.Add(pi.Name, If(Nullable.GetUnderlyingType(pi.PropertyType), pi.PropertyType)) Next 'Populate the table For Each item As T In collection Dim dr As DataRow = dt.NewRow() dr.BeginEdit() For Each pi As PropertyInfo In pia dr(pi.Name) = If(Nullable.GetUnderlyingType(pi.PropertyType) Is GetType(DateTime), DBNull.Value, pi.GetValue(item, Nothing)) Next dr.EndEdit() dt.Rows.Add(dr) Next Return dt End Function End Module 

我会search可空,并replace为一个string,可以为null不像DateTime。

 foreach (PropertyInfo pi in properties) { if (pi.PropertyType.Name.Contains("Nullable")) myDataType = typeof(String); else myDataType = pi.PropertyType; } 

这是一个完整的版本:

 private DataTable CreateDataTable(PropertyInfo[] properties) { DataTable dt = new DataTable(); DataColumn dc = null; foreach (PropertyInfo pi in properties) { dc = new DataColumn(); dc.ColumnName = pi.Name; if (pi.PropertyType.Name.Contains("Nullable")) dc.DataType = typeof(String); else dc.DataType = pi.PropertyType; // dc.DataType = pi.PropertyType; dt.Columns.Add(dc); } return dt; }