Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# NHibernate
Hi everyone,
 
I'm trying to get the dataTable using NHibernate cos I need to bind dataTable in reportviewer, but NHibernate always return IList and i don't know how to convert into DataTable, so if anyone knows, pls help me!!
 
   tdsReport.Purchase.Merge(purchaseDT); // ??? how to get DataTable using NHibernate
Posted 26-Mar-12 0:43am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try with this method
 
public static DataTable ToDataTable<t>(this IList<t> data)
    {
        PropertyDescriptorCollection properties = 
            TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                 row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }</t></t>
 
Source: http://stackoverflow.com/questions/564366/generic-list-to-datatable[^]
 

Update:
 
    public static class ExtensionMethods
    {
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
    }
 
and use it like this:
 
List<Purchase> a = new List<Purchase>();
DataTable t = a.ToDataTable<Purchase>();
 

Cheers
  Permalink  
v2
Comments
Doyle Raymond at 26-Mar-12 6:42am
   
if u don't mind, pls explain why PropertyDescriptorCollection is used and others ?? Thanks a lot, Mario Majcica...
:)
Mario Majcica at 26-Mar-12 7:30am
   
Well TypeDescriptor.GetProperties() method returns the collection of properties for a specified type (in this case type T (generic)) in form of a PropertyDescriptorCollection. Once we have all properties of your IList type, we will create a table and add a column for each property we retrieved earlier. Then we will recurese the data in the list and add a new row and respective data to the datatable. This is a generic extension method and can be used with any IList. Ex. DataTable t = a.ToDataTable<Purchase>(); where a is at ex. List<Purchase> a = new List<Purchase>();
 
Cheers
Doyle Raymond at 26-Mar-12 6:42am
   
or links i can learn ??
Mario Majcica at 26-Mar-12 7:43am
   
You can get more info's here:
 
http://www.codeproject.com/Articles/261639/Extension-Methods-in-NET
http://www.abhisheksur.com/2011/02/get-reflected-use-typedescriptor.html
 
etc.
 
What are you specifically interested in?
Doyle Raymond at 26-Mar-12 10:01am
   
by the way, thanks a lot for your help.. but i gave u wrong info that what i need is DataTable from DataSet.. not from System.Data.DataSet..
sorry, i don't know how to call it...
Convert IList into DataSet DataTable using NHibernate query...
and i am developing in Window Form...
:(
 
could u help again.. ?? Mario Majcica..
Mario Majcica at 26-Mar-12 10:06am
   
I have no idea of which other DataTable you are speaking. A code sample? or a namespace where this DataTable is defined?
Doyle Raymond at 26-Mar-12 10:12am
   
sorry, i don't know how to explain u...
excuse my bad english !!
I've tdsReport.xsd (DataSet)... and I'm currently developing window application which allows user to filter and display report...
what i need is get the DataTable from DataSet ,as I've mentioned, according to user wants using NHibernate and populate to reportViewer.. but what I know is that reportViewer's Merge Method wants DataSet and what i get from Nhibernate query is IList ... so I want to convert the IList into tdsReport.ReportDataTable... thanks for your help!!
Mario Majcica at 26-Mar-12 10:20am
   
Well this is the right code then! First convert your NHibernate IList object to DataTable. There should be an overload on Merge method http://msdn.microsoft.com/en-us/library/803bh6bc.aspx that accepts the DataTable as parameter. Be however sure that the table yoa are trying to merge has the same name as one contained into the DataSet (http://msdn.microsoft.com/en-us/library/w32ft3a8.aspx) and that both tables have the same structure, in other words both contain the same column's and same column types. Hope this helps.
 
Cheers
Doyle Raymond at 26-Mar-12 10:41am
   
I did but one error displayed ... :(
i declared like this..

public static tdsReport.PurchaseDataTable ToDataTable(this IList data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(t));
DataTable DT = new DataTable();
 
foreach (PropertyDescriptor prop in properties)
{
DT.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
 
foreach (T item in data)
{
DataRow row = DT.NewRow();
 
foreach (PropertyDescriptor prop2 in properties)
row[prop2.Name] = prop2.GetValue(item) ?? DBNull.Value;
DT.Rows.Add(row);
}
}
 
return DT as tdsReport.PurchaseDataTable;
}
Mario Majcica at 26-Mar-12 10:49am
   
Well you can't convert it to a strongly typed dataset in this way. Try another approach, based on the table that you need to merge, create a new instance of strongly typed dataset and fill it with the data from IList, then merge the two DataSets. You are missing some base concepts, start learning on how typed DataSet's work and then you can approach other problems. You can start from here http://support.microsoft.com/kb/320714 .
 
Cheers
Doyle Raymond at 26-Mar-12 10:44am
   
when i combined my solution...
 
"Extension method must be defined in a non-generic static class " error displayed..
 
:(
Doyle Raymond at 26-Mar-12 10:51am
   
thanks.. actually, i'm just a beginner in C#.. thanks 4 ur guide... I really appreciate it..
Mario Majcica at 26-Mar-12 11:19am
   
You're welcome! Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 26 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100