Unable to cast object of type ‘Oracle.DataAccess.Client.OracleParameter’ to type ‘System.IConvertible’.

Posted: January 26, 2011 in C#
Tags: , ,

Exception: System.InvalidCastException: Unable to cast object of type ‘Oracle.DataAccess.Client.OracleParameter’ to type ‘System.IConvertible’.
at System.Convert.ToDateTime(Object value)
at Oracle.DataAccess.Client.OracleParameter.PreBind_Date()
at Oracle.DataAccess.Client.OracleParameter.PreBind(OracleConnection conn, IntPtr errCtx, Int32 arraySize)
at Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery()

What this means?
‘System.IConvertible’ interface provides methods to convert the value of an instance of an implementing type to a common language runtime type like Boolean,Int32, UInt32, Int64,DateTime, Char,String etc that has an equivalent value.

public static OracleParameter AddInParameter(string paramName, OracleDbType paramType, object paramValue)
OracleParameter cmdParam = new OracleParameter(paramName, paramType);
cmdParam.Value = paramValue;
cmdParam.Direction = ParameterDirection.Input;
return cmdParam;

OracleParameter paramCreatedDate = new OracleParameter();
if (string.Equals(CREATION_DATE, DateTime.MinValue))
paramCreatedDate.Value = DateTime.Now;
paramCreatedDate.Value = CREATION_DATE;

Here in the below code it throws InvalidCastException “Unable to cast object of type ‘Oracle.DataAccess.Client.OracleParameter’ to type ‘System.IConvertible'”.

//Throws exception Unable to cast object of type
//'Oracle.DataAccess.Client.OracleParameter' to type 'System.IConvertible'.

What is the fix? And Why this occured?

It’s trying to convert OracleParameter to DateTime.If there is no meaningful conversion to a common language runtime type, then a particular interface method implementation throws InvalidCastException.You were supposed to pass paramCreatedDate.Value instead of paramCreatedDate


Hope this gives you some information on why InvalidCastException occurs & how to fix this for any type conversion similar to this 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s