1 //------------------------------------------------------------------------------
2 // <copyright file="DataError.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
10 namespace System.Data {
12 using System.Diagnostics;
15 /// <para>Represents an custom error that can be associated with a <see cref='System.Data.DataRow'/>.</para>
17 internal sealed class DataError {
18 private string rowError = String.Empty;
20 // column-level errors
22 private ColumnError[] errorList;
23 internal const int initialCapacity = 1;
25 internal DataError() {
28 internal DataError(string rowError) {
32 internal string Text {
41 internal bool HasErrors {
43 return(rowError.Length != 0 || count != 0);
48 // this method resets the error to the new value.
50 internal void SetColumnError(DataColumn column, string error) {
51 Debug.Assert(column != null, "Invalid (null) argument");
52 Debug.Assert(column.Table != null, "Invalid (loose) column");
53 if (error == null || error.Length == 0) {
54 // remove error from the collection
58 if (errorList == null) {
59 errorList = new ColumnError[initialCapacity];
61 int i = IndexOf(column);
62 errorList[i].column = column;
63 errorList[i].error = error;
70 internal string GetColumnError(DataColumn column) {
71 for (int i = 0; i < count; i++) {
72 if (errorList[i].column == column) {
73 return errorList[i].error;
79 internal void Clear(DataColumn column) {
83 for (int i = 0; i < count; i++) {
84 if (errorList[i].column == column) {
85 System.Array.Copy(errorList, i+1, errorList, i, count-i-1);
88 Debug.Assert(column.errors >= 0, "missing error counts");
93 internal void Clear() {
94 for (int i = 0; i < count; i++) {
95 errorList[i].column.errors--;
96 Debug.Assert(errorList[i].column.errors >= 0, "missing error counts");
99 rowError = String.Empty;
102 internal DataColumn[] GetColumnsInError() {
103 DataColumn[] cols = new DataColumn[count];
105 for (int i = 0; i < count; i++) {
106 cols[i] = errorList[i].column;
112 /// <para>Sets the error message for the <see cref='System.Data.DataError'/>.</para>
114 private void SetText(string errorText) {
115 if (null == errorText) {
116 errorText = String.Empty;
118 rowError = errorText;
121 internal int IndexOf (DataColumn column) {
122 // try to find the column
123 for (int i = 0; i < count; i++) {
124 if (errorList[i].column == column) {
129 if (count >= errorList.Length) {
130 int newCapacity = Math.Min(count*2, column.Table.Columns.Count);
131 ColumnError[] biggerList = new ColumnError[newCapacity];
132 System.Array.Copy(errorList, 0, biggerList, 0, count);
133 errorList = biggerList;
138 internal struct ColumnError {
139 internal DataColumn column;
140 internal string error;