Question

I have a stored proc that return's either a 1 or 0. I need this value back in the .net function that calls it. I know how to get a data set back from Oracle (use a refCursor), I know how to input data into Oracle, call cmd.ExecuteNonQuery() (assuming you are using a stored proc to input data). But how do you get a stored proc single return value? I am not returning a Data set but a 1 or a 0. So this is a little bit different.

Thanks.

UPDATE: CODE TO more specifically show where I am going wrong?

try
{
      using (OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["ACODBConnectionString3"].ConnectionString))
      {
                using (OracleCommand cmd = new OracleCommand(sProc,conn))
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    OracleParameter inputParm = new OracleParameter("HospitalFinIn", OracleDbType.Varchar2);
                    inputParm.Value = HosFin;
                    //inputParm3.Value = Double.Parse(isActive);
                    OracleParameter outRefParam = new OracleParameter("cur_out", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
                    //outRefParam.
                    cmd.Parameters.Add(inputParm);
                    cmd.Parameters.Add(outRefParam);
                    outcome = (int)cmd.ExecuteScalar();
                    if (outcome == 1) 
                        FinUniq = false;
                    else if (outcome == 0) 
                        FinUniq = true;
                    else 
                        FinUniq = true;
                    //outcome = cmd.ExecuteNonQuery();
                    //adapter = new OracleDataAdapter(cmd);
                    //adapter.Fill(ds);
                }
            }
        }

This is the exception I get when I run this

Specified cast is not valid.

Not sure what I am doing wrong. The stored proc definitely returns a 0 or 1.

Was it helpful?

Solution

Are you returning the scalar value via an OUT REFCURSOR using something similar to OPEN curInt FOR select 42 from dual?

You can obtain the scalar value by just executing the stored procedure using 'OracleCommand.ExecuteScalar()'. Remember to create an OracleParameter for the OUT refcursor and set is direction to 'ParameterDirection.Output' and the 'OracleDbType' property to 'OracleDbType.RefCursor'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top