2 // System.Data.Common.ExceptionHelper
\r
5 // Boris Kirzner (borisk@mainsoft.com)
\r
9 using System.Globalization;
\r
11 namespace System.Data.Common
\r
13 internal sealed class ExceptionHelper
\r
15 internal static ArgumentException InvalidSizeValue (int value)
\r
17 string [] args = new string [] {value.ToString ()};
\r
18 return new ArgumentException (GetExceptionMessage ("Invalid parameter Size value '{0}'. The value must be greater than or equal to 0.",args));
\r
21 internal static void CheckEnumValue (Type enumType, object value)
\r
23 if (!Enum.IsDefined (enumType, value))
\r
24 throw InvalidEnumValueException (enumType.Name, value);
\r
27 internal static ArgumentException InvalidEnumValueException (String enumeration, object value)
\r
29 string msg = string.Format (CultureInfo.InvariantCulture,
\r
30 "The {0} enumeration value, {1}, is invalid.",
\r
31 enumeration, value);
\r
33 return new ArgumentOutOfRangeException (enumeration, msg);
\r
35 return new ArgumentException (msg);
\r
39 internal static ArgumentOutOfRangeException InvalidDataRowVersion (DataRowVersion value)
\r
41 object [] args = new object [] { "DataRowVersion", value.ToString () } ;
\r
42 return new ArgumentOutOfRangeException (GetExceptionMessage ("{0}: Invalid DataRow Version enumeration value: {1}",args));
\r
45 internal static ArgumentOutOfRangeException InvalidParameterDirection (ParameterDirection value)
\r
47 object [] args = new object [] { "ParameterDirection", value.ToString () } ;
\r
48 return new ArgumentOutOfRangeException (GetExceptionMessage ("Invalid direction '{0}' for '{1}' parameter.",args));
\r
51 internal static InvalidOperationException NoStoredProcedureExists (string procedureName) {
\r
52 object [] args = new object [1] { procedureName } ;
\r
53 return new InvalidOperationException (GetExceptionMessage ("The stored procedure '{0}' doesn't exist.", args));
\r
56 internal static ArgumentNullException ArgumentNull (string parameter)
\r
58 return new ArgumentNullException (parameter);
\r
61 internal static InvalidOperationException TransactionRequired ()
\r
63 return new InvalidOperationException (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."));
\r
66 internal static ArgumentOutOfRangeException InvalidOleDbType (int value)
\r
68 string [] args = new string [] { value.ToString() };
\r
69 return new ArgumentOutOfRangeException (GetExceptionMessage ("Invalid OleDbType enumeration value: {0}",args));
\r
72 internal static ArgumentException InvalidDbType(int value)
\r
74 string [] args = new string [] { value.ToString () };
\r
75 return new ArgumentException (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));
\r
78 internal static InvalidOperationException DeriveParametersNotSupported(Type type,CommandType commandType)
\r
80 string [] args = new string [] { type.ToString(),commandType.ToString() };
\r
81 return new InvalidOperationException (GetExceptionMessage ("{0} DeriveParameters only supports CommandType.StoredProcedure, not CommandType.{1}.",args));
\r
84 internal static InvalidOperationException ReaderClosed (string mehodName)
\r
86 string [] args = new string [] { mehodName };
\r
87 return new InvalidOperationException (GetExceptionMessage ("Invalid attempt to {0} when reader is closed.",args));
\r
90 internal static ArgumentOutOfRangeException InvalidSqlDbType (int value)
\r
92 string [] args = new string [] { value.ToString () };
\r
93 return new ArgumentOutOfRangeException (GetExceptionMessage ("{0}: Invalid SqlDbType enumeration value: {1}.",args));
\r
96 internal static ArgumentException UnknownDataType (string type1, string type2)
\r
98 string [] args = new string [] { type1, type2 };
\r
99 return new ArgumentException (GetExceptionMessage ("No mapping exists from DbType {0} to a known {1}.",args));
\r
102 internal static InvalidOperationException TransactionNotInitialized ()
\r
104 return new InvalidOperationException (GetExceptionMessage ("Execute requires the command to have a transaction object when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."));
\r
107 internal static InvalidOperationException TransactionNotUsable (Type type)
\r
109 return new InvalidOperationException (string.Format (
\r
110 CultureInfo.InvariantCulture,
\r
111 "This {0} has completed; it is no longer usable.",
\r
115 internal static InvalidOperationException ParametersNotInitialized (int parameterPosition,string parameterName,string parameterType)
\r
117 object [] args = new object [] { parameterPosition, parameterName, parameterType };
\r
118 return new InvalidOperationException (GetExceptionMessage ("Parameter {0}: '{1}', the property DbType is uninitialized: OleDbType.{2}.",args));
\r
121 internal static InvalidOperationException WrongParameterSize(string provider)
\r
123 string [] args = new string [] { provider };
\r
124 return new InvalidOperationException (GetExceptionMessage ("{0}.Prepare method requires all variable length parameters to have an explicitly set non-zero Size.",args));
\r
127 internal static InvalidOperationException ConnectionNotOpened (string operationName, string connectionState)
\r
129 object [] args = new object [] { operationName, connectionState };
\r
130 return new InvalidOperationException (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));
\r
133 internal static InvalidOperationException ConnectionNotInitialized (string methodName)
\r
135 object [] args = new object [] { methodName };
\r
136 return new InvalidOperationException (GetExceptionMessage ("{0}: Connection property has not been initialized.",args));
\r
139 internal static InvalidOperationException OpenConnectionRequired (string methodName, object connectionState)
\r
141 object [] args = new object [] { methodName, connectionState };
\r
142 return new InvalidOperationException (GetExceptionMessage ("{0} requires an open and available Connection. The connection's current state is {1}.",args));
\r
145 internal static InvalidOperationException OpenedReaderExists ()
\r
147 return new InvalidOperationException (GetExceptionMessage ("There is already an open DataReader associated with this Connection which must be closed first."));
\r
150 internal static InvalidOperationException ConnectionAlreadyOpen (object connectionState)
\r
152 object [] args = new object [] { connectionState };
\r
153 return new InvalidOperationException (GetExceptionMessage ("The connection is already Open (state={0}).",args));
\r
156 internal static InvalidOperationException ConnectionClosed ()
\r
158 return new InvalidOperationException ("Invalid operation. The Connection is closed.");
\r
161 internal static InvalidOperationException ConnectionStringNotInitialized ()
\r
163 return new InvalidOperationException (GetExceptionMessage ("The ConnectionString property has not been initialized."));
\r
166 internal static InvalidOperationException ConnectionIsBusy (object commandType,object connectionState)
\r
168 object [] args = new object [] { commandType.ToString (), connectionState.ToString () };
\r
169 return new InvalidOperationException (GetExceptionMessage ("The {0} is currently busy {1}.",args));
\r
172 internal static InvalidOperationException NotAllowedWhileConnectionOpen (string propertyName, object connectionState)
\r
174 object [] args = new object [] { propertyName, connectionState };
\r
175 return new InvalidOperationException (GetExceptionMessage ("Not allowed to change the '{0}' property while the connection (state={1}).",args));
\r
178 internal static ArgumentException OleDbNoProviderSpecified ()
\r
180 return new ArgumentException (GetExceptionMessage ("An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'."));
\r
183 internal static ArgumentException InvalidValueForKey (string key)
\r
185 string [] args = new string [] { key };
\r
186 return new ArgumentException (String.Format ("Invalid value for key {0}",args));
\r
189 internal static InvalidOperationException ParameterSizeNotInitialized( int parameterIndex, string parameterName,string parameterType,int parameterSize)
\r
191 object [] args = new object [] { parameterIndex.ToString (), parameterName, parameterType, parameterSize.ToString () };
\r
192 return new InvalidOperationException (GetExceptionMessage ("Parameter {0}: '{1}' of type: {2}, the property Size has an invalid size: {3}",args));
\r
195 internal static ArgumentException InvalidUpdateStatus (UpdateStatus status)
\r
197 object [] args = new object [] { status };
\r
198 return new ArgumentException (GetExceptionMessage ("Invalid UpdateStatus: {0}",args));
\r
201 internal static InvalidOperationException UpdateRequiresCommand (string command)
\r
203 object [] args = new object [] { command };
\r
204 return new InvalidOperationException (GetExceptionMessage ("Auto SQL generation during {0} requires a valid SelectCommand.",args));
\r
207 internal static DataException RowUpdatedError ()
\r
209 return new DataException (GetExceptionMessage ("RowUpdatedEvent: Errors occurred; no additional is information available."));
\r
212 internal static ArgumentNullException CollectionNoNullsAllowed (object collection, object objectsType)
\r
214 object [] args = new object [] {collection.GetType ().ToString (), objectsType.ToString ()};
\r
215 return new ArgumentNullException (GetExceptionMessage ("The {0} only accepts non-null {1} type objects."));
\r
218 internal static ArgumentException CollectionAlreadyContains(object objectType,string propertyName, object propertyValue, object collection)
\r
220 object [] args = new object [] {objectType.ToString (), propertyName, propertyValue, collection.GetType ().ToString ()};
\r
221 return new ArgumentException (GetExceptionMessage ("The {0} with {1} '{2}' is already contained by this {3}.",args));
\r
224 internal static string GetExceptionMessage (string exceptionMessage,object [] args)
\r
226 if ((args == null) || (args.Length == 0)) {
\r
227 return exceptionMessage;
\r
229 return String.Format (exceptionMessage,args);
\r
233 internal static string GetExceptionMessage (string exceptionMessage)
\r
235 return GetExceptionMessage (exceptionMessage, null);
\r