enter image description hereI am trying to convert my List
to DataTable
but while converting the records count gets 0.
Here is my code:
DataAccessProvider dap = new DataAccessProvider(
Settings.Default.SQLServerConnection,
DatabaseType.MSSql);
var employees = dap.SelectAll("Employees").AsEnumerable().ToList();
DataTable Employees = new DataTable();
Employees = ToDataTable(employees);
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(
BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Setting column names as Property names
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
public List<dynamic> SelectAll(string tableName)
{
List<dynamic> result = new List<dynamic>();
using (IDataReader reader = ExecuteReader("Select * From " + tableName))
{
while (reader.Read())
{
dynamic expando = new ExpandoObject();
for (int i = 0; i < reader.FieldCount; i++)
{
string columnName = reader.GetName(i);
((IDictionary<String, Object>)expando).Add(
columnName,
reader[columnName]);
}
result.Add(expando);
}
}
return result;
}
All above are my code. When I debugged I found that in ToDataTable function the Props count gets 0. So what is wrong here?