0x80070005 - JavaScript runtime error: Access is denied.

How to create a DataTable from a class?

Using Reflection to create a DataTable from a Class?

//How to create a DataTable from a Class?
public static DataTable CreateDataTable<T>(IEnumerable<T> list)
    Type type = typeof(T);
    var properties = type.GetProperties();

    DataTable dataTable = new DataTable();
    foreach (PropertyInfo propertyinfo in properties)
        dataTable.Columns.Add(new DataColumn(propertyinfo.Name, Nullable.GetUnderlyingType(propertyinfo.PropertyType) ?? propertyinfo.PropertyType));

    foreach (T entity in list)
        object[] values = new object[properties.Length];
        for (int i = 0; i < properties.Length; i++)
            values[i] = properties[i].GetValue(entity);


    return dataTable;


Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions