DataSet nie obsługuje systemu.Nullable< > w eksporcie
Próbowałem wygenerować raport za pomocą eksportu do Excell, PDF, TextFile. Robię to w MVC. Mam klasę, którą nazwałem SPBatch (która jest dokładną nazwą mojej procedury składowanej w moim SQL) i zawiera ona:
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; }
Jak widzisz niektóre z moich kolumn są zadeklarowane jako Nullable. Poszło gładko z wyszukiwania i wyświetlania wyników w tabeli. Mam kilka przycisków poniżej, które są przyciskami obrazu do eksportu i za każdym razem, gdy próbuję wyeksportować w Excelu, always get the problem "DataSet does not support System.Nullable " w tej części mojego kodu:
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);
}
}
Błąd pojawia się na tym z komentarzem. Pomożesz mi, co mam zrobić? Próbowałem dodać DBNull w moim kodzie, ale nadal dostaję ten sam błąd. Próbowałem usunąć Nullable w moim SPBatch, ale dostaję błąd, że niektóre tabele muszą być zadeklarowane jako Nullable.
Co mam zrobić?3 answers
Spróbuj z
dt.Columns.Add(pi.Name, Nullable.GetUnderlyingType(
pi.PropertyType) ?? pi.PropertyType);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-23 01:49:07
Dzięki wersji C# generowania datatable i kilku hacking wokół, mogę zaoferować tę odpowiedź w VB-umieściłem go tutaj, ponieważ miałem wiele kłopotów, chcąc uzyskać filtrowalny dataset z przechowywanego proc podczas korzystania z prostego datalayer. Mam nadzieję, że to pomoże komuś innemu!
Uwaga: przypadek użycia jest tam, gdzie chcesz użyć BindingSource.Filter = "some query 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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-20 05:32:31
Szukałbym nullable i zamieniłbym go na łańcuch, który może być null w przeciwieństwie do DateTime.
foreach (PropertyInfo pi in properties)
{
if (pi.PropertyType.Name.Contains("Nullable"))
myDataType = typeof(String);
else
myDataType = pi.PropertyType;
}
Oto pełna wersja:
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-23 02:08:19