Convert IEnumerable to DataTable

Look at this one: Convert List/IEnumerable to DataTable/DataView In my code I changed it into a extension method: public static DataTable ToDataTable<T>(this List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach(var prop in props) { tb.Columns.Add(prop.Name, prop.PropertyType); } foreach (var item in items) { var values = new object[props.Length]; … Read more

SqlBulkCopy – The given value of type String from the data source cannot be converted to type money of the specified target column

For the people stumbling across this question and getting a similar error message in regards to an nvarchar instead of money: The given value of type String from the data source cannot be converted to type nvarchar of the specified target column. This could be caused by a too-short column. For example, if your column … Read more

Export DataTable to Excel with EPPlus

using (ExcelPackage pck = new ExcelPackage(newFile)) { ExcelWorksheet ws = pck.Workbook.Worksheets.Add(“Accounts”); ws.Cells[“A1”].LoadFromDataTable(dataTable, true); pck.Save(); } That should do the trick for you. If your fields are defined as int EPPlus will properly cast the columns into a number or float.

How do you Sort a DataTable given column and direction?

I assume “direction” is “ASC” or “DESC” and dt contains a column named “colName” public static DataTable resort(DataTable dt, string colName, string direction) { DataTable dtOut = null; dt.DefaultView.Sort = colName + ” ” + direction; dtOut = dt.DefaultView.ToTable(); return dtOut; } OR without creating dtOut public static DataTable resort(DataTable dt, string colName, string direction) … Read more

How to add New Column with Value to the Existing DataTable?

Without For loop: Dim newColumn As New Data.DataColumn(“Foo”, GetType(System.String)) newColumn.DefaultValue = “Your DropDownList value” table.Columns.Add(newColumn) C#: System.Data.DataColumn newColumn = new System.Data.DataColumn(“Foo”, typeof(System.String)); newColumn.DefaultValue = “Your DropDownList value”; table.Columns.Add(newColumn);

.NET – Convert Generic Collection to DataTable

Then presumably you’ll need to lift them to the non-nullable form, using Nullable.GetUnderlyingType, and perhaps change a few null values to DbNull.Value… Change the assignment to be: row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; and when adding the columns to be: table.Columns.Add(prop.Name, Nullable.GetUnderlyingType( prop.PropertyType) ?? prop.PropertyType); And it works. (?? is the null-coalescing operator; it uses the … Read more

How to convert a column of DataTable to a List

Try this: static void Main(string[] args) { var dt = new DataTable { Columns = { { “Lastname”,typeof(string) }, { “Firstname”,typeof(string) } } }; dt.Rows.Add(“Lennon”, “John”); dt.Rows.Add(“McCartney”, “Paul”); dt.Rows.Add(“Harrison”, “George”); dt.Rows.Add(“Starr”, “Ringo”); List<string> s = dt.AsEnumerable().Select(x => x[0].ToString()).ToList(); foreach(string e in s) Console.WriteLine(e); Console.ReadLine(); }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)