DataTable load() constraint error DataTable load() constraint error database database

DataTable load() constraint error


Per the documentation for DataTable.Load Method (IDataReader, LoadOption), I suspect that you may be encountering the behavior described in the excerpt below. Have you checked the number of columns returned from your query versus the number of columns on your DataTable? Does the name of the column returned from the query match the desired column name in your DataTable?

Condition: The schemas are compatible, but the loaded result set schema contains fewer columns than does the DataTable.

Behavior: If a missing column has a default value defined or the column's data type is nullable, the Load method allows the rows to be added, substituting the default or null value for the missing column. If no default value or null can be used, then the Load method throws an exception. If no specific default value has been supplied, the Load method uses the null value as the implied default value.

Your code in the while loop is probably working because it makes no attempt to match schema. It just fills values positionally and will succeed as long as the types are compatible, no constraints are violated, and the array doesn't contain more items than the row has columns.