Q:
The following code:
var dtInstTotal = dtExternal.AsEnumerable()
.Union(dtEmployed.AsEnumerable())
.OrderBy(d => d.Field<string>("emp_name"));
dtInst = dtInstTotal.CopyToDataTable();//exception
throw an exception:
Value was either too large or too small for an Int16.Couldn’t store
<103930> in emp_num Column. Expected type is Int16. —>
System.OverflowException: Value was either too large or too small for
an Int16.
I suspect that
dtExternalhas ashorttype foremp_num, whereasdtEmployedhas some other type (int,longor maybe justushort) – or maybe vice versa.CopyToDataTablejust uses the types from the first table containing the first row it sees, and then it’s having problems when it comes across a value for a column with the same name from a different table. From the docs:Basically: make sure your two original tables have the same schema.
EDIT: We don’t know what your methods to populate the two original
DataTables look like – but you may find that by creating theDataTablefirst, explicitly setting the type ofemp_num, and then filling the table, that will be okay.You could even leave your original methods alone, and build a new
DataTablewith the right schema, then call