1

I wrote query which I got no return value as follows:

  con.Open();
  OracleCommand  cmd= CreateCommand();
  cmd.CommandText = $"INSERT INTO {"Customer".GetDoubleQuoted()}({"City".GetDoubleQuoted()},{"CompanyName".GetDoubleQuoted()} , {"ContactName".GetDoubleQuoted()} ,{"Country".GetDoubleQuoted()},{"CustomerID".GetDoubleQuoted()},{"Phone".GetDoubleQuoted()}) VALUES ('Chicago', 'Amisys','Jwk', 'USA','aaa', '123456') \n Returning {"City".GetDoubleQuoted()} into :city";
  cmd.Connection = con;
  cmd.Parameters.Add(new OracleParameter
  {
       ParameterName = ":city",
       OracleDbType = OracleDbType.Varchar2,
       Direction = ParameterDirection.Output
   });
   cmd.ExecuteNonQuery();
  var value= cmd.Parameters[":city"].Value.ToString();

Could someone help me finding out why value doesn't come out?

Thanks in advance. joon

2
  • Try setting the ParameterDirection to ReturnValue Commented Jan 29, 2018 at 18:45
  • Thanks Roy, but it doesn't make any change at all. Commented Feb 2, 2018 at 1:26

1 Answer 1

1

The error is a small gotcha in ODP.NET, everytime you receive a varchar2 returnValue parameter you must specify the size. So simply adding the line Size=32000, to your example will make it work.

See the full code bellow:

 con.Open();
 OracleCommand  cmd= CreateCommand();
 cmd.CommandText = $@"INSERT INTO {"Customer".GetDoubleQuoted()(
        {"City".GetDoubleQuoted()},
        {"CompanyName".GetDoubleQuoted()},
        {"ContactName".GetDoubleQuoted()},
        {"Country".GetDoubleQuoted()},
        {"CustomerID".GetDoubleQuoted()},
        {"Phone".GetDoubleQuoted()}
     ) VALUES ('Chicago', 'Amisys','Jwk', 'USA','aaa', '123456')
     Returning {"City".GetDoubleQuoted()} into :city";
 cmd.Connection = con;
 cmd.Parameters.Add(new OracleParameter
     {
         ParameterName = ":city",
         OracleDbType = OracleDbType.Varchar2,
         Size = 32000,
         Direction = ParameterDirection.ReturnValue
     });
 cmd.ExecuteNonQuery();
 var value= cmd.Parameters[":city"].Value.ToString();

Note: 32000 is the maximum size for a varchar2 column.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Roy, I got return value as expected. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.