[SOLVED] oracle function always return null

Issue

This Content is from Stack Overflow. Question asked by Nebro Prog

I’m using the flowing code to call the oracle function with the return value,
but its return null always

OracleCommand cmd = new OracleCommand();
using (OracleConnection cnn = new OracleConnection(“Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=321352427544)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)));User ID=abc;Password=123;”))
{

        cmd.Connection = cnn;
        cmd.CommandText = "GetEmp";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("P_EMP_ID", OracleDbType.Int32).Value = 4241;
    

        cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;

        cnn.Open();
        cmd.ExecuteNonQuery();

        string Count = (string)cmd.Parameters["return_value"].Value;

        cnn.Close();
    }



Solution

the problem was solved in two ways by

  1. add cmd.BindByName = true;
  2. add the return parameter in the first: cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;


This Question was asked in StackOverflow by Nebro Prog and Answered by Nebro Prog It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?