I have an xsd DataSet Schema which allows me to define a the structure for a DataTable
. For every column, I've set a default value so when constructing a new row, I get instantiated values ("" for strings and 0 for Ints) instead of DbNull
I'd like to preserve that when using either DataTable.Load
or SqlDataAdapter.Fill
as in either of the two following method calls
Dim table1 As New CodeSetSchemas.EntityByRoleDataTable()
Using reader As SqlDataReader = cmd.ExecuteReader()
table1.Load(reader)
End Using
Dim table2 As New CodeSetSchemas.EntityByRoleDataTable()
Using adapter As New SqlDataAdapter(cmd)
adapter.Fill(table2)
End Using
But when either of these methods create a new row, they actually write DbNull from the database into the row. If I set the AllowDBNull
property on each column to False
, I get the following exception:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Is there a way to preserve my default values for each row when filling a DataTable?