Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 35863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:18:10+00:00 2026-05-10T14:18:10+00:00

Does anyone have any tips for dealing with ConstraintExceptions thrown by XSD datasets? This

  • 0

Does anyone have any tips for dealing with ConstraintExceptions thrown by XSD datasets?

This is the exception with the cryptic message:

System.Data.ConstraintException : Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:18:10+00:00Added an answer on May 10, 2026 at 2:18 pm

    A couple of tips that I’ve found lately.

    1. It’s much better to use the TableAdapter FillByDataXXXX() methods instead of GetDataByXXXX() methods because the DataTable passed into the fill method can be interrogated for clues:

      • DataTable.GetErrors() returns an array of DataRow instances in error
      • DataRow.RowError contains a description of the row error
      • DataRow.GetColumnsInError() returns an array of DataColumn instances in error
    2. Recently, I wrapped up some interrogation code into a subclass of ConstraintException that’s turned out to be a useful starting point for debugging.

    C# Example usage:

    Example.DataSet.fooDataTable table = new DataSet.fooDataTable();  try {     tableAdapter.Fill(table); } catch (ConstraintException ex) {     // pass the DataTable to DetailedConstraintException to get a more detailed Message property     throw new DetailedConstraintException('error filling table', table, ex); } 

    Output:

    DetailedConstraintException : table fill failed
    Errors reported for ConstraintExceptionHelper.DataSet+fooDataTable [foo]
    Columns in error: [1]
    [PRODUCT_ID] – total rows affected: 1085
    Row errors: [4]
    [Column ‘PRODUCT_ID’ is constrained to be unique. Value ‘1’ is already present.] – total rows affected: 1009
    [Column ‘PRODUCT_ID’ is constrained to be unique. Value ‘2’ is already present.] – total rows affected: 20
    [Column ‘PRODUCT_ID’ is constrained to be unique. Value ‘4’ is already present.] – total rows affected: 34
    [Column ‘PRODUCT_ID’ is constrained to be unique. Value ‘6’ is already present.] – total rows affected: 22
    —-> System.Data.ConstraintException : Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

    I don’t know if this is too much code to include in a Stack Overflow answer but here’s the C# class in full. Disclaimer: this works for me, please feel free to use/modify as appropriate.

    using System; using System.Collections.Generic; using System.Text; using System.Data;  namespace ConstraintExceptionHelper {      /// <summary>     /// Subclass of ConstraintException that explains row and column errors in the Message property     /// </summary>     public class DetailedConstraintException : ConstraintException     {          private const int InitialCountValue = 1;           /// <summary>         /// Initialises a new instance of DetailedConstraintException with the specified string and DataTable         /// </summary>         /// <param name='message'>exception message</param>         /// <param name='ErroredTable'>DataTable in error</param>         public DetailedConstraintException(string message, DataTable erroredTable)             : base(message)         {             ErroredTable = erroredTable;         }           /// <summary>         /// Initialises a new instance of DetailedConstraintException with the specified string, DataTable and inner Exception         /// </summary>         /// <param name='message'>exception message</param>         /// <param name='ErroredTable'>DataTable in error</param>         /// <param name='inner'>the original exception</param>         public DetailedConstraintException(string message, DataTable erroredTable, Exception inner)             : base(message, inner)         {             ErroredTable = erroredTable;         }           private string buildErrorSummaryMessage()         {             if (null == ErroredTable) { return 'No errored DataTable specified'; }             if (!ErroredTable.HasErrors) { return 'No Row Errors reported in DataTable=[' + ErroredTable.TableName + ']'; }              foreach (DataRow row in ErroredTable.GetErrors())             {                 recordColumnsInError(row);                 recordRowsInError(row);             }              StringBuilder sb = new StringBuilder();              appendSummaryIntro(sb);             appendErroredColumns(sb);             appendRowErrors(sb);              return sb.ToString();         }           private void recordColumnsInError(DataRow row)         {             foreach (DataColumn column in row.GetColumnsInError())             {                 if (_erroredColumns.ContainsKey(column.ColumnName))                 {                     _erroredColumns[column.ColumnName]++;                     continue;                 }                  _erroredColumns.Add(column.ColumnName, InitialCountValue);             }         }           private void recordRowsInError(DataRow row)         {             if (_rowErrors.ContainsKey(row.RowError))             {                 _rowErrors[row.RowError]++;                 return;             }              _rowErrors.Add(row.RowError, InitialCountValue);         }           private void appendSummaryIntro(StringBuilder sb)         {             sb.AppendFormat('Errors reported for {1} [{2}]{0}', Environment.NewLine, ErroredTable.GetType().FullName, ErroredTable.TableName);         }           private void appendErroredColumns(StringBuilder sb)         {             sb.AppendFormat('Columns in error: [{1}]{0}', Environment.NewLine, _erroredColumns.Count);              foreach (string columnName in _erroredColumns.Keys)             {                 sb.AppendFormat('\t[{1}] - rows affected: {2}{0}',                                 Environment.NewLine,                                 columnName,                                 _erroredColumns[columnName]);             }         }           private void appendRowErrors(StringBuilder sb)         {             sb.AppendFormat('Row errors: [{1}]{0}', Environment.NewLine, _rowErrors.Count);              foreach (string rowError in _rowErrors.Keys)             {                 sb.AppendFormat('\t[{1}] - rows affected: {2}{0}',                                 Environment.NewLine,                                 rowError,                                 _rowErrors[rowError]);             }         }           /// <summary>         /// Get the DataTable in error         /// </summary>         public DataTable ErroredTable         {             get { return _erroredTable; }             private set { _erroredTable = value; }         }           /// <summary>         /// Get the original ConstraintException message with extra error information         /// </summary>         public override string Message         {             get { return base.Message + Environment.NewLine + buildErrorSummaryMessage(); }         }           private readonly SortedDictionary<string, int> _rowErrors = new SortedDictionary<string, int>();         private readonly SortedDictionary<string, int> _erroredColumns = new SortedDictionary<string, int>();         private DataTable _erroredTable;     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The first parameter to GetDlgItemInt should be the handle to… May 12, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Books Online is a bit indecipherable on the subject of… May 12, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Basically, you can very well function with one "central" GitHub… May 12, 2026 at 8:03 pm

Related Questions

Does anyone have a elegant way of dealing with errors in ASP.Net MVC? I
Does anyone have any tips for debugging exceptions in a C# object initializer block?
Anyone got tips for diagnosing SharePoint / ASP.Net Request Timed Out messages? We've recently
This is a question for anyone who has the pleasure to work in both

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.