Convert OracleDbType.Decimal to C# decimal

  • Thread starter Thread starter MartinKK
  • Start date Start date
M

MartinKK

Hi all,
I have a problem with converting OracleDbType.Decimal to System.Decimal. I
have the following code:
....(class)
public decimal speed;
OracleCommand cmd = new OracleCommand("GETNORM", dataCa.connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("return", OracleDbType.Int32).Direction =
ParameterDirection.ReturnValue;
cmd.Parameters.Add("CODE", OracleDbType.Decimal).Value = idSubject;
cmd.Parameters.Add("OUTSPEED", OracleDbType.Decimal).Direction =
ParameterDirection.Output;
cmd.ExecuteNonQuery();
this.speed = (Decimal)cmd.Parameters["OUTSPEEDVALUE"].Value;
....
The last line causes runtime error: Specified type-cast is invalid (I'm
translating from Czech language). In VS2008 debug I see, that
"cmd.Parameters["OUTSPEEDVALUE"].Value" has value (OUTSPEEDVALUE = 62).
My question is: How can I assign value from Oracle parameter to .NET (C#)
variable.
I'm unhappy 2 days.
Thanks
Martin
 
MartinKK said:
Hi all,
I have a problem with converting OracleDbType.Decimal to System.Decimal. I
have the following code:
....(class)
public decimal speed;
OracleCommand cmd = new OracleCommand("GETNORM", dataCa.connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("return", OracleDbType.Int32).Direction =
ParameterDirection.ReturnValue;
cmd.Parameters.Add("CODE", OracleDbType.Decimal).Value = idSubject;
cmd.Parameters.Add("OUTSPEED", OracleDbType.Decimal).Direction =
ParameterDirection.Output;
cmd.ExecuteNonQuery();
this.speed = (Decimal)cmd.Parameters["OUTSPEEDVALUE"].Value;
....
The last line causes runtime error: Specified type-cast is invalid (I'm
translating from Czech language). In VS2008 debug I see, that
"cmd.Parameters["OUTSPEEDVALUE"].Value" has value (OUTSPEEDVALUE = 62).
My question is: How can I assign value from Oracle parameter to .NET (C#)
variable.
I'm unhappy 2 days.
Thanks
Martin

What's the data type of cmd.Parameters["OUTSPEEDVALUE"].Value? (quick
watch or assign to Object variable) I don't see a special OracleDecimal
data type inside the System.Data.OracleClient namespace. Only
OracleNumber, but there, conversion to Decimal is defined and should
work then.


Armin
 
Armin Zingler said:
MartinKK said:
Hi all,
I have a problem with converting OracleDbType.Decimal to System.Decimal. I
have the following code:
....(class)
public decimal speed;
OracleCommand cmd = new OracleCommand("GETNORM", dataCa.connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("return", OracleDbType.Int32).Direction =
ParameterDirection.ReturnValue;
cmd.Parameters.Add("CODE", OracleDbType.Decimal).Value = idSubject;
cmd.Parameters.Add("OUTSPEED", OracleDbType.Decimal).Direction =
ParameterDirection.Output;
cmd.ExecuteNonQuery();
this.speed = (Decimal)cmd.Parameters["OUTSPEEDVALUE"].Value;
....
The last line causes runtime error: Specified type-cast is invalid (I'm
translating from Czech language). In VS2008 debug I see, that
"cmd.Parameters["OUTSPEEDVALUE"].Value" has value (OUTSPEEDVALUE = 62).
My question is: How can I assign value from Oracle parameter to .NET (C#)
variable.
I'm unhappy 2 days.
Thanks
Martin

What's the data type of cmd.Parameters["OUTSPEEDVALUE"].Value? (quick
watch or assign to Object variable) I don't see a special OracleDecimal
data type inside the System.Data.OracleClient namespace. Only
OracleNumber, but there, conversion to Decimal is defined and should
work then.


Armin
Hi,
I'm sorry, there is error. Right is:
cmd.Parameters.Add("OUTSPEEDVALUE", OracleDbType.Decimal).Direction =
ParameterDirection.Output;
Martin
 
MartinKK said:
What's the data type of cmd.Parameters["OUTSPEEDVALUE"].Value? (quick
watch or assign to Object variable) I don't see a special OracleDecimal
data type inside the System.Data.OracleClient namespace. Only
OracleNumber, but there, conversion to Decimal is defined and should
work then.
Hi,
I'm sorry, there is error. Right is:
cmd.Parameters.Add("OUTSPEEDVALUE", OracleDbType.Decimal).Direction =
ParameterDirection.Output;
Martin

Yes, but it doesn't answer my question.

Armin
 
Have you tried this.speed = (Decimal)(OracleDecimal)cmd.Parameters["OUTSPEEDVALUE"].Value;

The first () calls a type conversion to OracleDecimal then the second converts to Decimal.

There is also the Convert.ToDecimal(cmd.Parameters["OUTSPEEDVALUE"].Value) which will skip the double type conversion.

Give them a try.
 
Last edited:
Back
Top