Select Page
This entry has been published on 2016-01-07 and may be out of date.

Last Updated on 2016-01-07.

By default, MS Visual Studio debugging message does not show useful details if e.g. the Fill() method of a DataSet/DataTable causes a ConstraintException . Especially if your table has a lot of columns, indexes, foreign keys etc., finding the cause can take a long time.

But if you catch the Exception and use its GetErrors() method, you will get more information in detail.

Example:

try
            {
                this.yourTableAdapter.FillById(this.yourtable, id);
               

            }
            catch (ConstraintException)
            {
                DataRow[] rowErrors = db.yourtable.GetErrors();

                System.Diagnostics.Debug.WriteLine("Errors:"
                    + rowErrors.Length);

                for (int i = 0; i < rowErrors.Length; i++)
                {
                    System.Diagnostics.Debug.WriteLine(rowErrors[i].RowError);

                    foreach (DataColumn col in rowErrors[i].GetColumnsInError())
                    {
                        System.Diagnostics.Debug.WriteLine(col.ColumnName
                            + ":" + rowErrors[i].GetColumnError(col));
                    }
                }
            }