You've been very helpful, can you just go away now.

Sometimes when dealing with a typed dataset (a good idea) you have a problem with constraints, or with nullable fields, or with other stuff. Microsoft gives you a less-than-helpful exception that says "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
Right...
Well, if you use:

ds.EnforceConstraints = false;

then you can call this method which will output the table, row, column, and exact constraint that was violated.


public void GetDataSetErrors(DataSet ds){

try { ds.EnforceConstraints = true; }

catch(Exception ex) {

foreach
(DataTable table in ds.Tables) {

foreach (DataRow row in table.GetErrors()){ System.Diagnostics.Trace.WriteLine("Table: "+
table.TableName); System.Diagnostics.Trace.WriteLine(ex.Message
); System.Diagnostics.Trace.WriteLine("Row: "
+row.RowError); foreach (DataColumn col in
row.GetColumnsInError())
{ System.Diagnostics.Trace.WriteLine("Column
" +
col.ColumnName); System.Diagnostics.Trace.WriteLine(
row.GetColumnError(col)); } } } } }


It just makes you feel all warm and fuzzy inside, doesn't it?

Comments

Popular posts from this blog

Database Projects, SQL Unit Tests, and TeamCity

Brent: Programmer. Gamer. Cheapskate. All around good guy.

Building nice XML from SQL Server Tables