Posts Tagged ‘exception’

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;
else
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'.
AddInParameter("I_DATE",OracleDbType.Date,paramCreatedDate)

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


AddInParameter("I_DATE",OracleDbType.Date,paramCreatedDate.Value)

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

Advertisements