Did you ever set EnforceConstraints to true and encountered an exception and tried to find out which constraint exactly failed ?
Dave has written a small function that simply but cleverly iterates over all tables/columns in case of an exception and displays the exact locations constraints couldn't be enforced:
If you prefer C#:
/// Safely tries to enable constraints, and if not possible returns detailed error
public static void TryEnforceConstraints(DataSet ds)
{
try
{
ds.EnforceConstraints = true;
}
catch (Exception ex)
{
Debug.WriteLine("DataSet errors: " + ds.DataSetName);
foreach (DataTable table in ds.Tables)
{
DataRow[] ErrorRows = table.GetErrors();
foreach (DataRow row in ErrorRows)
{
Debug.WriteLine("Table: " + table.TableName);
Debug.WriteLine(" Row Error: " + row.RowError);
DataColumn[] ErrorColumns = row.GetColumnsInError();
foreach (DataColumn column in ErrorColumns)
{
Debug.WriteLine("Column: " + column.ColumnName);
Debug.WriteLine(" Error: " + row.GetColumnError(column));
}
}
}
}
}
Not advanced magic here, but practical coding help :-) Happy Coding !
Friday, April 18, 2008
Subscribe to:
Posts (Atom)