code_junkie

Handling XSD Dataset ConstraintExceptions

Nov 10th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. System.Data.ConstraintException : Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
  2.  
  3. Example.DataSet.fooDataTable table = new DataSet.fooDataTable();
  4.  
  5. try
  6. {
  7. tableAdapter.Fill(table);
  8. }
  9. catch (ConstraintException ex)
  10. {
  11. // pass the DataTable to DetailedConstraintException to get a more detailed Message property
  12. throw new DetailedConstraintException("error filling table", table, ex);
  13. }
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using System.Data;
  19.  
  20. namespace ConstraintExceptionHelper
  21. {
  22.  
  23. /// <summary>
  24. /// Subclass of ConstraintException that explains row and column errors in the Message property
  25. /// </summary>
  26. public class DetailedConstraintException : ConstraintException
  27. {
  28.  
  29. private const int InitialCountValue = 1;
  30.  
  31.  
  32. /// <summary>
  33. /// Initialises a new instance of DetailedConstraintException with the specified string and DataTable
  34. /// </summary>
  35. /// <param name="message">exception message</param>
  36. /// <param name="ErroredTable">DataTable in error</param>
  37. public DetailedConstraintException(string message, DataTable erroredTable)
  38. : base(message)
  39. {
  40. ErroredTable = erroredTable;
  41. }
  42.  
  43.  
  44. /// <summary>
  45. /// Initialises a new instance of DetailedConstraintException with the specified string, DataTable and inner Exception
  46. /// </summary>
  47. /// <param name="message">exception message</param>
  48. /// <param name="ErroredTable">DataTable in error</param>
  49. /// <param name="inner">the original exception</param>
  50. public DetailedConstraintException(string message, DataTable erroredTable, Exception inner)
  51. : base(message, inner)
  52. {
  53. ErroredTable = erroredTable;
  54. }
  55.  
  56.  
  57. private string buildErrorSummaryMessage()
  58. {
  59. if (null == ErroredTable) { return "No errored DataTable specified"; }
  60. if (!ErroredTable.HasErrors) { return "No Row Errors reported in DataTable=[" + ErroredTable.TableName + "]"; }
  61.  
  62. foreach (DataRow row in ErroredTable.GetErrors())
  63. {
  64. recordColumnsInError(row);
  65. recordRowsInError(row);
  66. }
  67.  
  68. StringBuilder sb = new StringBuilder();
  69.  
  70. appendSummaryIntro(sb);
  71. appendErroredColumns(sb);
  72. appendRowErrors(sb);
  73.  
  74. return sb.ToString();
  75. }
  76.  
  77.  
  78. private void recordColumnsInError(DataRow row)
  79. {
  80. foreach (DataColumn column in row.GetColumnsInError())
  81. {
  82. if (_erroredColumns.ContainsKey(column.ColumnName))
  83. {
  84. _erroredColumns[column.ColumnName]++;
  85. continue;
  86. }
  87.  
  88. _erroredColumns.Add(column.ColumnName, InitialCountValue);
  89. }
  90. }
  91.  
  92.  
  93. private void recordRowsInError(DataRow row)
  94. {
  95. if (_rowErrors.ContainsKey(row.RowError))
  96. {
  97. _rowErrors[row.RowError]++;
  98. return;
  99. }
  100.  
  101. _rowErrors.Add(row.RowError, InitialCountValue);
  102. }
  103.  
  104.  
  105. private void appendSummaryIntro(StringBuilder sb)
  106. {
  107. sb.AppendFormat("Errors reported for {1} [{2}]{0}", Environment.NewLine, ErroredTable.GetType().FullName, ErroredTable.TableName);
  108. }
  109.  
  110.  
  111. private void appendErroredColumns(StringBuilder sb)
  112. {
  113. sb.AppendFormat("Columns in error: [{1}]{0}", Environment.NewLine, _erroredColumns.Count);
  114.  
  115. foreach (string columnName in _erroredColumns.Keys)
  116. {
  117. sb.AppendFormat("t[{1}] - rows affected: {2}{0}",
  118. Environment.NewLine,
  119. columnName,
  120. _erroredColumns[columnName]);
  121. }
  122. }
  123.  
  124.  
  125. private void appendRowErrors(StringBuilder sb)
  126. {
  127. sb.AppendFormat("Row errors: [{1}]{0}", Environment.NewLine, _rowErrors.Count);
  128.  
  129. foreach (string rowError in _rowErrors.Keys)
  130. {
  131. sb.AppendFormat("t[{1}] - rows affected: {2}{0}",
  132. Environment.NewLine,
  133. rowError,
  134. _rowErrors[rowError]);
  135. }
  136. }
  137.  
  138.  
  139. /// <summary>
  140. /// Get the DataTable in error
  141. /// </summary>
  142. public DataTable ErroredTable
  143. {
  144. get { return _erroredTable; }
  145. private set { _erroredTable = value; }
  146. }
  147.  
  148.  
  149. /// <summary>
  150. /// Get the original ConstraintException message with extra error information
  151. /// </summary>
  152. public override string Message
  153. {
  154. get { return base.Message + Environment.NewLine + buildErrorSummaryMessage(); }
  155. }
  156.  
  157.  
  158. private readonly SortedDictionary<string, int> _rowErrors = new SortedDictionary<string, int>();
  159. private readonly SortedDictionary<string, int> _erroredColumns = new SortedDictionary<string, int>();
  160. private DataTable _erroredTable;
  161. }
  162. }
Add Comment
Please, Sign In to add comment