Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert OracleParameter.Value to Int32

Tags:

c#

casting

oracle

I have a stored procedure call that goes like this:

using (OracleConnection con = new OracleConnection(ConfigurationManager.AppSettings["Database"]))
using (OracleCommand cmd = new OracleCommand("Package.Procedure", con))
{
   Int32 existsCount;

   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("successCount", OracleDbType.Int32, 0, ParameterDirection.InputOutput);
   cmd.Parameters.Add("BusinessId", OracleDbType.Int64, listRec.BusinessId, ParameterDirection.Input);

    con.Open();
    cmd.ExecuteScalar();
    con.Close();

    existsCount = Convert.ToInt32(cmd.Parameters["successCount"].Value);

    return (existsCount);
}

But on this line:

existsCount = Convert.ToInt32(cmd.Parameters["successCount"].Value);

It throws the Exception "Unable to cast object of type 'Oracle.DataAccess.Types.OracleDecimal' to type 'System.IConvertible'."

Any thoughts? Thanks.

like image 654
Felix Martinez Avatar asked Jun 11 '10 20:06

Felix Martinez


2 Answers

You can also try:

Oracle.DataAccess.Types.OracleDecimal d = (Oracle.DataAccess.Types.OracleDecimal)cmd.Parameters["successCount"].Value;  if( d.IsNull )     existsCount = 0; else     existsCount = d.ToInt32( ); 
like image 91
Karim Budhwani Avatar answered Sep 20 '22 18:09

Karim Budhwani


What about

existsCount = int.Parse(cmd.Parameters["successCount"].Value.ToString());
like image 30
Jürgen Steinblock Avatar answered Sep 21 '22 18:09

Jürgen Steinblock