I'm having difficulty trying to call my DBA's function inside C#. It works fine in the Oracle query browser, however when I attempt to execute it in C# it fails.
The most common error is : "ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 1"
I've attempted to increase the size of the C# parameters but to no success. Not sure what I'm doing wrong.
C# Code :
string returnValue = string.Empty;
using (OracleConnection cn = new OracleConnection("ConnectionString"))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "package.function";
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter param1 = new OracleParameter();
OracleParameter param2 = new OracleParameter();
OracleParameter param3 = new OracleParameter();
OracleParameter param4 = new OracleParameter();
OracleParameter param5 = new OracleParameter();
param1.OracleDbType = OracleDbType.Varchar2;
param1.Direction = ParameterDirection.Input;
param1.Value = "Test808";
param1.ParameterName = "var1";
//param1.Size = 2000;
param2.OracleDbType = OracleDbType.Varchar2;
param2.Direction = ParameterDirection.Input;
param2.Value = "68B54814";
param2.ParameterName = "var2";
//param2.Size = 2000;
param3.OracleDbType = OracleDbType.Varchar2;
param3.Direction = ParameterDirection.Input;
param3.Value = "71839";
param3.ParameterName = "var4";
//param3.Size = 2000;
param4.OracleDbType = OracleDbType.Varchar2;
param4.Direction = ParameterDirection.Input;
param4.Value = 55 ;
param4.ParameterName = "var4";
//param4.Size = 2000;
param5.OracleDbType = OracleDbType.Varchar2;
param5.Direction = ParameterDirection.ReturnValue;
param5.ParameterName = "return";
//param5.Size = 2000;
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);
cmd.Parameters.Add(param4);
cmd.Parameters.Add(param5);
try
{
cn.Open();
cmd.ExecuteNonQuery();
returnValue = cmd.Parameters["return"].Value.ToString();
}
catch
{
}
finally
{
cn.Close();
}
}
}
return returnValue;
Function Header:
FUNCTION create_rec
(p_mims_pallet_id varchar2
,p_item_no varchar2
,p_packer_id varchar2
,p_tare_wt varchar2
)
return varchar2
1st time trying Oracle calls. Never had an issue with SQL. The parameter.Size is commented out for not seeming to do anything.
Was able to get it working by removing the individual Parameters and creating them the Parameter.Add method.
Working Code:
string returnValue = string.Empty;
using (OracleConnection cn = new OracleConnection("ConString"))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "package.function";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("rv", OracleDbType.Varchar2, 200, "", ParameterDirection.ReturnValue);
cmd.Parameters.Add("var1", OracleDbType.Varchar2, 20, "Test808", ParameterDirection.Input);
cmd.Parameters.Add("var2", OracleDbType.Varchar2, 20, "68B54814", ParameterDirection.Input);
cmd.Parameters.Add("var3", OracleDbType.Varchar2, 20, "71839", ParameterDirection.Input);
cmd.Parameters.Add("var4", OracleDbType.Decimal, 55, ParameterDirection.Input);
try
{
cn.Open();
cmd.ExecuteNonQuery();
returnValue = cmd.Parameters["rv"].Value.ToString();
}
catch
{
}
finally
{
cn.Close();
}
}
}
return returnValue;
I fixed this issue by changing its syntax. It was giving numeric or value overflow error if we use below syntax:
cmd.Parameters.Add("strSqlMsg", OracleDbType.Varchar2, 255, ParameterDirection.Output);
Which I changed to below and it worked.
cmd.Parameters.Add("strSqlMsg", OracleDbType.Varchar2, ParameterDirection.Output).Size=255;
Hope this will work for you.
Thanks.
Atul
string strRetrun = string.Empty;
using (OracleConnection objCon = (OracleConnection)_connection)
{
using (OracleCommand objCom = new OracleCommand())
{
objCom.Connection = objCon;
objCom.CommandText = "SAM.PKG_SAM_ECH.F_FUND_TRANSFER_TYPE";
objCom.CommandType = CommandType.StoredProcedure;
OracleParameter codeReturn = new OracleParameter("RETURN", OracleType.VarChar, 1000);
codeReturn.Direction = ParameterDirection.ReturnValue;
OracleParameter code1 = new OracleParameter("V_RECORD_ID", OracleType.Number);
code1.Direction = ParameterDirection.Input;
if (obj.RecordId != null)
code1.Value = obj.RecordId;
else
code1.Value = DBNull.Value;
OracleParameter code2 = new OracleParameter("V_DEBIT_APAC", OracleType.VarChar, 200);
code2.Direction = ParameterDirection.Input;
if (obj.P_BENEF_APAC != null)
code2.Value = obj.P_BENEF_APAC;
else
code2.Value = DBNull.Value;
OracleParameter code3 = new OracleParameter("V_ACCOUNT_TYPE", OracleType.VarChar, 200);
code3.Direction = ParameterDirection.Input;
if (obj.BenfAccType != null)
code3.Value = obj.BenfAccType;
else
code3.Value = DBNull.Value;
OracleParameter code4 = new OracleParameter("V_IFSC_CODE", OracleType.VarChar, 200);
code4.Direction = ParameterDirection.Input;
if (obj.IFSC != null)
code4.Value = obj.IFSC;
else
code4.Value = DBNull.Value;
objCom.Parameters.Add(codeReturn);
objCom.Parameters.Add(code1);
objCom.Parameters.Add(code2);
objCom.Parameters.Add(code3);
objCom.Parameters.Add(code4);
try
{
objCon.Open();
objCom.ExecuteNonQuery();
strRetrun = objCom.Parameters["RETURN"].Value.ToString();
}
catch
{
}
finally
{
objCon.Close();
}
}
}
return strRetrun;
To everybody who is wondering why the answer from Tim B is working.
It's the order of the parameters!
The return parameter of the function has to be bound first!
Also for the other Parameters the order counts and not the name.
The first version with creating the parameters individually, would be OK if "param5" would have been added as first object in to the collection.
I got stuck on this for while :-(
Related
I have created a PL/SQL function and stored it in my oracle database.Now I want to call that function on clicking a button.
I am using visual studio and c#.
My humble attempt-
private void button3_Click(object sender, EventArgs e)
{
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
String x1 = textBox1.Text;
String x2 = textBox2.Text;
comm.CommandText = "log_in";
comm.Parameters.Add(new OracleParameter("c", OracleDbType.Varchar2, textBox1.Text, ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter("tt", OracleDbType.Varchar2, 256, ParameterDirection.ReturnValue));
String z = comm.Parameters["z"].Value.ToString();
comm.ExecuteNonQuery();
if (z.Equals('1'))
{ MessageBox.Show("correct"); }
conn.Close();
}
PL/SQL-
CREATE OR REPLACE FUNCTION log_in
(
x IN VARCHAR2,
y IN VARCHAR2
) RETURN VARCHAR2 AS
match_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO match_count
FROM student_login
WHERE email = x
AND password = y;
IF match_count = 0 THEN
RETURN 0;
ELSE
RETURN 1;
END IF;
END;
note-my data source is .NET framework data provider for oracle and the answer is for odp.net
Your function has two input parameters, thus your call must also define two input parameter plus the return parameter. Method Add(OracleParameter) is possible but redundant.
It should be like this:
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
comm.CommandText = "log_in";
comm.Parameters.Add("x", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox1.Text;
// As far as I remember "ParameterDirection.Input" is the default, so you may skip it
comm.Parameters.Add("y", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox2.Text;
comm.Parameters.Add("ret", OracleDbType.Byte, ParameterDirection.ReturnValue);
comm.Parameters["ret"].DbType = DbType.Byte;
comm.ExecuteNonQuery();
String returnValue = comm.Parameters["ret"].Value.ToString();
Actually I never used CommandType.StoredProcedure. If the code above does not work try this one instead:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := log_in(:x, :y); END;";
Hi I had some problems with StoredProcedure. And finally get it working.
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
comm.CommandText = "log_in";
// Important is to add returnValue as described bellow
OracleParameter retval = new OracleParameter("ret", OracleDbType.Varchar2 , 20);
retval.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(retval);
comm.Parameters.Add("x", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox1.Text;
comm.Parameters.Add("y", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox2.Text;
comm.ExecuteNonQuery();
String returnValue = comm.Parameters["ret"].Value.ToString();
Need Help! I am new in executing function in a stored procedure package, I need to execute this package using oracle C# windows application..
CREATE OR REPLACE PACKAGE TSCBE1005_PKG IS
FUNCTION TSCBE1005_001_FNC( P_BLD_LVL_CD IN VARCHAR2,
P_IN_DATE IN VARCHAR2,
P_IN_PC IN VARCHAR2,
P_BLD_SPEC_NO IN OUT VARCHAR2,
P_BLD_SPEC_NO_SEQ IN OUT NUMBER,
P_ART_NO IN OUT VARCHAR2`,
P_CURE_KIBN IN OUT VARCHAR2,
P_CURE_LR_KBN IN OUT VARCHAR2,
P_HINMEI IN OUT VARCHAR2,
P_TIRE_SIZE IN OUT VARCHAR2,
P_E4_MARK IN OUT NUMBER,
P_SECTOR_NO IN OUT VARCHAR2,
P_UPLATE_NO IN OUT VARCHAR2,
P_LPLATE_NO IN OUT VARCHAR2,
P_BLADER_NO IN OUT VARCHAR2,
P_MAX_TCLC_SINGLE IN OUT NUMBER,
P_TCLC_UNIT_SINGLE IN OUT VARCHAR2,
P_MAX_IP_SINGLE IN OUT NUMBER,
P_IP_UNIT_SINGLE IN OUT VARCHAR2,
P_PR IN OUT NUMBER,
P_MSG IN OUT VARCHAR2
) RETURN NUMBER;
PROCEDURE TSCBE1005_002_PRC( P_RET_DATA IN NUMBER,
P_INFO1 IN VARCHAR2,
P_INFO2 IN VARCHAR2,
P_IN_PC IN VARCHAR2
) ;
END TSCBE1005_PKG;
When I'm running it to Oracle, using script below, it's actually successful.
variable O_BLD_SPEC_NO VARCHAR2(7)
variable O_BLD_SPEC_NO_SEQ NUMBER
variable O_ART_NO VARCHAR2(5)
variable O_CURE_KIBN VARCHAR2(4)
variable O_CURE_LR_KBN VARCHAR2(1)
variable O_HINMEI VARCHAR2(50)
variable O_TIRE_SIZE VARCHAR2(20)
variable O_E4_MARK NUMBER
variable O_SECTOR_NO VARCHAR2(6)
variable O_UPLATE_NO VARCHAR2(6)
variable O_LPLATE_NO VARCHAR2(6)
variable O_BLADER_NO VARCHAR2(7)
variable O_MAX_TCLC_SINGLE NUMBER
variable O_TCLC_UNIT_SINGLE VARCHAR2(3)
variable O_MAX_IO_SINGLE NUMBER
variable O_IO_UNIT_SINGLE VARCHAR2(3)
variable O_PR NUMBER
variable O_MSG VARCHAR2(20)
variable ret varchar2(100)
execute :ret := TSCBE1005_PKG.TSCBE1005_001_FNC('R123456789','20170124120644','YRC07400',:O_BLD_SPEC_NO,:O_BLD_SPEC_NO_SEQ,:O_ART_NO,:O_CURE_KIBN,:O_CURE_LR_KBN,:O_HINMEI,:O_TIRE_SIZE,:O_E4_MARK,:O_SECTOR_NO,:O_UPLATE_NO,:O_LPLATE_NO,:O_BLADER_NO,:O_MAX_TCLC_SINGLE,:O_TCLC_UNIT_SINGLE,:O_MAX_IO_SINGLE,:O_IO_UNIT_SINGLE,:O_PR,:O_MSG)
;
print O_BLD_SPEC_NO
print O_BLD_SPEC_NO_SEQ
print O_ART_NO
print O_CURE_KIBN
print O_CURE_LR_KBN
print O_HINMEI
print O_TIRE_SIZE
print O_E4_MARK
print O_SECTOR_NO
print O_UPLATE_NO
print O_LPLATE_NO
print O_BLADER_NO
print O_MAX_TCLC_SINGLE
print O_TCLC_UNIT_SINGLE
print O_MAX_IO_SINGLE
print O_IO_UNIT_SINGLE
print O_PR
print O_MSG
print ret
I tried to execute this in C# oracle using below code,
using (OracleConnection objConn = new OracleConnection("User id=skraze; Password=skraze; Data source=database"))
{
objConn.Open();
OracleCommand objCmd = new OracleCommand();
objCmd.Connection = objConn;
objCmd.CommandText = "TSCBE1005_PKG.TSCBE1005_001_FNC";
objCmd.CommandType = CommandType.StoredProcedure;
OracleParameter P_BLD_LVL_CD = new OracleParameter();
P_BLD_LVL_CD.ParameterName = ":P_BLD_LVL_CD";
P_BLD_LVL_CD.OracleType = OracleType.VarChar;
P_BLD_LVL_CD.Direction = ParameterDirection.Input;
P_BLD_LVL_CD.Value = "R123456789";
objCmd.Parameters.Add(P_BLD_LVL_CD);
OracleParameter P_IN_DATE = new OracleParameter();
P_IN_DATE.ParameterName = ":P_IN_DATE";
P_IN_DATE.OracleType = OracleType.VarChar;
P_IN_DATE.Direction = ParameterDirection.Input;
P_IN_DATE.Value = "20170124120644";
objCmd.Parameters.Add(P_IN_DATE);
OracleParameter P_IN_PC = new OracleParameter();
P_IN_PC.ParameterName = ":P_IN_PC";
P_IN_PC.OracleType = OracleType.VarChar;
P_IN_PC.Direction = ParameterDirection.Input;
P_IN_PC.Value = "YRC07400";
objCmd.Parameters.Add(P_IN_PC);
OracleParameter P_BLD_SPEC_NO = new OracleParameter();
P_BLD_SPEC_NO.ParameterName = ":P_BLD_SPEC_NO";
P_BLD_SPEC_NO.OracleType = OracleType.VarChar;
P_BLD_SPEC_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLD_SPEC_NO);
OracleParameter P_BLD_SPEC_NO_SEQ = new OracleParameter();
P_BLD_SPEC_NO_SEQ.ParameterName = ":P_BLD_SPEC_NO_SEQ";
P_BLD_SPEC_NO_SEQ.OracleType = OracleType.Number;
P_BLD_SPEC_NO_SEQ.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLD_SPEC_NO_SEQ);
OracleParameter P_ART_NO = new OracleParameter();
P_ART_NO.ParameterName = ":P_ART_NO";
P_ART_NO.OracleType = OracleType.VarChar;
P_ART_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_ART_NO);
OracleParameter P_CURE_KIBN = new OracleParameter();
P_CURE_KIBN.ParameterName = ":P_CURE_KIBN";
P_CURE_KIBN.OracleType = OracleType.VarChar;
P_CURE_KIBN.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_CURE_KIBN);
OracleParameter P_CURE_LR_KBN = new OracleParameter();
P_CURE_LR_KBN.ParameterName = ":P_CURE_LR_KBN";
P_CURE_LR_KBN.OracleType = OracleType.VarChar;
P_CURE_LR_KBN.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_CURE_LR_KBN);
OracleParameter P_HINMEI = new OracleParameter();
P_HINMEI.ParameterName = ":P_HINMEI";
P_HINMEI.OracleType = OracleType.VarChar;
P_HINMEI.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_HINMEI);
OracleParameter P_TIRE_SIZE = new OracleParameter();
P_TIRE_SIZE.ParameterName = ":P_TIRE_SIZE";
P_TIRE_SIZE.OracleType = OracleType.VarChar;
P_TIRE_SIZE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_TIRE_SIZE);
OracleParameter P_E4_MARK = new OracleParameter();
P_E4_MARK.ParameterName = ":P_E4_MARK";
P_E4_MARK.OracleType = OracleType.Number;
P_E4_MARK.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_E4_MARK);
OracleParameter P_SECTOR_NO = new OracleParameter();
P_SECTOR_NO.ParameterName = ":P_SECTOR_NO";
P_SECTOR_NO.OracleType = OracleType.VarChar;
P_SECTOR_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_SECTOR_NO);
OracleParameter P_UPLATE_NO = new OracleParameter();
P_UPLATE_NO.ParameterName = ":P_UPLATE_NO";
P_UPLATE_NO.OracleType = OracleType.VarChar;
P_UPLATE_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_UPLATE_NO);
OracleParameter P_LPLATE_NO = new OracleParameter();
P_LPLATE_NO.ParameterName = ":P_LPLATE_NO";
P_LPLATE_NO.OracleType = OracleType.VarChar;
P_LPLATE_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_LPLATE_NO);
OracleParameter P_BLADER_NO = new OracleParameter();
P_BLADER_NO.ParameterName = ":P_BLADER_NO";
P_BLADER_NO.OracleType = OracleType.VarChar;
P_BLADER_NO.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_BLADER_NO);
OracleParameter P_MAX_TCLC_SINGLE = new OracleParameter();
P_MAX_TCLC_SINGLE.ParameterName = ":P_MAX_TCLC_SINGLE";
P_MAX_TCLC_SINGLE.OracleType = OracleType.Number;
P_MAX_TCLC_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_MAX_TCLC_SINGLE);
OracleParameter P_TCLC_UNIT_SINGLE = new OracleParameter();
P_TCLC_UNIT_SINGLE.ParameterName = ":P_TCLC_UNIT_SINGLE";
P_TCLC_UNIT_SINGLE.OracleType = OracleType.VarChar;
P_TCLC_UNIT_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_TCLC_UNIT_SINGLE);
OracleParameter P_MAX_IO_SINGLE = new OracleParameter();
P_MAX_IO_SINGLE.ParameterName = ":P_MAX_IO_SINGLE";
P_MAX_IO_SINGLE.OracleType = OracleType.Number;
P_MAX_IO_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_MAX_IO_SINGLE);
OracleParameter P_IO_UNIT_SINGLE = new OracleParameter();
P_IO_UNIT_SINGLE.ParameterName= ":P_IO_UNIT_SINGLE";
P_IO_UNIT_SINGLE.OracleType = OracleType.VarChar;
P_IO_UNIT_SINGLE.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_IO_UNIT_SINGLE);
OracleParameter P_PR = new OracleParameter();
P_PR.ParameterName = ":P_PR";
P_PR.OracleType = OracleType.Number;
P_PR.Direction = ParameterDirection.Input;
objCmd.Parameters.Add(P_PR);
OracleParameter W_BLD_SPEC_NO = new OracleParameter();
W_BLD_SPEC_NO.OracleType = OracleType.VarChar;
W_BLD_SPEC_NO.Size = 7;
W_BLD_SPEC_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLD_SPEC_NO);
OracleParameter W_BLD_SPEC_NO_SEQ = new OracleParameter();
W_BLD_SPEC_NO_SEQ.OracleType = OracleType.Number;
W_BLD_SPEC_NO_SEQ.Size = 1;
W_BLD_SPEC_NO_SEQ.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLD_SPEC_NO_SEQ);
OracleParameter W_ART_NO = new OracleParameter();
W_ART_NO.OracleType = OracleType.VarChar;
W_ART_NO.Size = 5;
W_ART_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_ART_NO);
OracleParameter W_CURE_KIBN = new OracleParameter();
W_CURE_KIBN.OracleType = OracleType.VarChar;
W_CURE_KIBN.Size = 4;
W_CURE_KIBN.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_CURE_KIBN);
OracleParameter W_CURE_LR_KBN = new OracleParameter();
W_CURE_LR_KBN.OracleType = OracleType.VarChar;
W_CURE_LR_KBN.Size = 1;
W_CURE_LR_KBN.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_CURE_LR_KBN);
OracleParameter W_HINMEI = new OracleParameter();
W_HINMEI.OracleType = OracleType.VarChar;
W_HINMEI.Size = 50;
W_HINMEI.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_HINMEI);
OracleParameter W_TIRE_SIZE = new OracleParameter();
W_TIRE_SIZE.OracleType = OracleType.VarChar;
W_TIRE_SIZE.Size = 20;
W_TIRE_SIZE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_TIRE_SIZE);
OracleParameter W_E4_MARK = new OracleParameter();
W_E4_MARK.OracleType = OracleType.Number;
W_E4_MARK.Size = 1;
W_E4_MARK.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_E4_MARK);
OracleParameter W_SECTOR_NO = new OracleParameter();
W_SECTOR_NO.OracleType = OracleType.VarChar;
W_SECTOR_NO.Size = 6;
W_SECTOR_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_SECTOR_NO);
OracleParameter W_UPLATE_NO = new OracleParameter();
W_UPLATE_NO.OracleType = OracleType.VarChar;
W_UPLATE_NO.Size = 6;
W_UPLATE_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_UPLATE_NO);
OracleParameter W_LPLATE_NO = new OracleParameter();
W_LPLATE_NO.OracleType = OracleType.VarChar;
W_LPLATE_NO.Size = 6;
W_LPLATE_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_LPLATE_NO);
OracleParameter W_BLADER_NO = new OracleParameter();
W_BLADER_NO.OracleType = OracleType.VarChar;
W_BLADER_NO.Size = 7;
W_BLADER_NO.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_BLADER_NO);
OracleParameter W_MAX_TCLC_SINGLE = new OracleParameter();
W_MAX_TCLC_SINGLE.OracleType = OracleType.Number;
W_MAX_TCLC_SINGLE.Size = 1;
W_MAX_TCLC_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_MAX_TCLC_SINGLE);
OracleParameter W_TCLC_UNIT_SINGLE = new OracleParameter();
W_TCLC_UNIT_SINGLE.OracleType = OracleType.VarChar;
W_TCLC_UNIT_SINGLE.Size = 3;
W_TCLC_UNIT_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_TCLC_UNIT_SINGLE);
OracleParameter W_MAX_IO_SINGLE = new OracleParameter();
W_MAX_IO_SINGLE.OracleType = OracleType.Number;
W_MAX_IO_SINGLE.Size = 1;
W_MAX_IO_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_MAX_IO_SINGLE);
OracleParameter W_IO_UNIT_SINGLE = new OracleParameter();
W_IO_UNIT_SINGLE.OracleType = OracleType.VarChar;
W_IO_UNIT_SINGLE.Size = 3;
W_IO_UNIT_SINGLE.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_IO_UNIT_SINGLE);
OracleParameter W_PR = new OracleParameter();
W_PR.OracleType = OracleType.Number;
W_PR.Size = 1;
W_PR.Direction = ParameterDirection.ReturnValue;
objCmd.Parameters.Add(W_PR);
objCmd.ExecuteNonQuery();
objConn.Close();
}
But I'm still getting error
{"ORA-06550: line 1, column 34:\nPLS-00103: Encountered the symbol \"=\" when expecting one of the following:\n\n . ( * # % & = - + ; < / > at in is mod not rem\n <an exponent (**)> <> or != or ~= >= <= <> and or like\n between || indicator\n"}
Please, I hope anyone could help me :(
This looks wrong
P_BLD_LVL_CD.ParameterName = ":P_BLD_LVL_CD";
^
The colon is used for identifying placeholders in a SQL string. Actual parameter name should be
P_BLD_LVL_CD.ParameterName = "P_BLD_LVL_CD";
Also ParameterDirection.ReturnValue is used for the return value only. So there should only be one such parameter per function call. You appear to be using it for all the IN OUT parameters. That is wrong.
For IN OUT parameters you need to use ParameterDirection.InputOutput.
Incidentally a function with IN OUT parameters is poor PL/SQL practice, but I guess there's not much you can do about that.
So, TSCBE1005_001_FNC is a function which takes three IN (read only) parameters and eighteen IN OUT (read/write) parameters. Therefore your call needs:
one parameter with ParameterDirection.ReturnValue
three parameters with ParameterDirection.Input
eighteen parameters with ParameterDirection.InputOutput
I never used objCmd.CommandType = CommandType.StoredProcedure;
I always use this syntax:
objCmd.CommandText = "BEGIN :res := TSCBE1005_PKG.TSCBE1005_001_FNC(:P_BLD_LVL_CD, :P_IN_DATE, ...); END;";
objCmd.CommandType = CommandType.Text;
Then your parameter declaration is wrong.
P_SECTOR_NO.Direction = ParameterDirection.Input;
ParameterDirection has to match signature of your function, i.e. ReturnValue or Input or Output (not used in your example) or InputOutput
The type is also wrong:
P_SECTOR_NO.OracleType = OracleType.VarChar;
You must use OracleDbType, see OracleDbType Enumeration. For mapping between OracleDbType and .NET type see C#: Oracle Data Type Equivalence with OracleDbType
btw, you can put this all in one command, e.g.
cmd.Parameters.Add("P_BLD_LVL_CD", OracleDbType.Varchar2, ParameterDirection.Input).Value = "R035570412";
Be aware in case of ParameterDirection.Output you have to set DbType separately. Also in case of OracleDbType.Varchar2 you must specify the size and an initial value:
cmd.Parameters.Add("parName", OracleDbType.Varchar2, 240, null, ParameterDirection.Output);
cmd.Parameters("parName").DbType = DbType.String;
I am stuck on an issue for past few days.The issue is detailed below:
There are 15 input params (of type int and varchar), 1 record type param and 5 output params for this oracle package.
It was observed that while passing record type parameter to the package this exception is thrown,work fine with those packages with out record type param.
Exception Thrown:-Unable to cast object of type 'System.Int32' to type
'System.Array'. Stack Trace:- at
Oracle.DataAccess.Client.OracleParameter.SetStatus(Int32 arraySize) at
Oracle.DataAccess.Client.OracleParameter.ResetCtx(Int32 arraySize) at
Oracle.DataAccess.Client.OracleParameter.PreBind(OracleConnection
conn, IntPtr errCtx, Int32 arraySize) at
Oracle.DataAccess.Client.OracleCommand.ExecuteNonQuery() at
POC_App.DataformatConverter.ExecuteCreateSOPackage()
What I have tried:
public static void ExecuteCreateSOPackage()
{
string constr = EstablishDBConnection();
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand cmd = new OracleCommand("APPS.XX_OM_CREATE_SO_TEST.create_sale_orders", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
cmd.ArrayBindCount = 100;
OracleParameter retVal = new OracleParameter("retVal", OracleDbType.RefCursor);
retVal.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(retVal);
OracleParameter paramPersonId = setInputParameters("p_person_id", OracleDbType.Int32, 0);
paramPersonId.Value = 31842;
cmd.Parameters.Add(paramPersonId);
<likewise 14 more input params >
---------------------------------
//Record type parameter..
OracleParameter p_so_lines_tab = new OracleParameter();
p_so_lines_tab.ParameterName = "p_so_lines_tab";
p_so_lines_tab.OracleDbType = OracleDbType.NVarchar2;
p_so_lines_tab.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
p_so_lines_tab.Size = 3;
p_so_lines_tab.Direction = ParameterDirection.Input;
OracleParameter P_SO_LINES_TAB_ITEM = new OracleParameter();
P_SO_LINES_TAB_ITEM.ParameterName = "p_so_lines_tab_item";
P_SO_LINES_TAB_ITEM.OracleDbType = OracleDbType.NVarchar2;
P_SO_LINES_TAB_ITEM.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
P_SO_LINES_TAB_ITEM.Size = 1000;
P_SO_LINES_TAB_ITEM.Direction = ParameterDirection.Input;
OracleParameter QUANTITY = new OracleParameter();
QUANTITY.ParameterName = "quantity";
QUANTITY.OracleDbType = OracleDbType.Int32;
QUANTITY.Value = 1;
OracleParameter QUANTITY_UOM = new OracleParameter();
QUANTITY_UOM.ParameterName = "quantity_uom";
QUANTITY_UOM.OracleDbType = OracleDbType.Varchar2;
QUANTITY_UOM.Value = "Ea";
OracleParameter ITEM_ID = new OracleParameter();
ITEM_ID.ParameterName = "item_id";
ITEM_ID.OracleDbType = OracleDbType.Int32;
ITEM_ID.Value = 162744;
OracleParameter PROMISE_DATE = new OracleParameter();
PROMISE_DATE.ParameterName = "promise_date";
PROMISE_DATE.OracleDbType = OracleDbType.Date;
PROMISE_DATE.Value = "2016-04-01T08:58:50.649Z";
OracleParameter SCHEDULE_SHIP_DATE = new OracleParameter();
SCHEDULE_SHIP_DATE.ParameterName = "schedule_ship_date";
SCHEDULE_SHIP_DATE.OracleDbType = OracleDbType.Date;
SCHEDULE_SHIP_DATE.Value = "2016-04-01T08:58:50.649Z";
OracleParameter CURRENCY_CODE = new OracleParameter();
CURRENCY_CODE.ParameterName = "currency_code";
CURRENCY_CODE.OracleDbType = OracleDbType.Varchar2;
CURRENCY_CODE.Value = "USD";
OracleParameter UNIT_SELLING_PRICE = new OracleParameter();
UNIT_SELLING_PRICE.ParameterName = "unit_selling_price";
UNIT_SELLING_PRICE.OracleDbType = OracleDbType.Int32;
UNIT_SELLING_PRICE.Value = 200;
OracleParameter SALESREP_ID = new OracleParameter();
SALESREP_ID.ParameterName = "salesrep_id";
SALESREP_ID.OracleDbType = OracleDbType.Int32;
SALESREP_ID.Value = 1412;
P_SO_LINES_TAB_ITEM.Value = new object[] { QUANTITY, QUANTITY_UOM, ITEM_ID, PROMISE_DATE, SCHEDULE_SHIP_DATE, CURRENCY_CODE, UNIT_SELLING_PRICE, SALESREP_ID };
//Adding to outer most collection
p_so_lines_tab.Value = P_SO_LINES_TAB_ITEM;
cmd.Parameters.Add(p_so_lines_tab);
cmd.Parameters.Add("x_time_stamp", OracleDbType.TimeStamp, ParameterDirection.Output);
cmd.Parameters.Add("x_order_header_id", OracleDbType.Int32, ParameterDirection.Output);
cmd.Parameters.Add("x_order_number", OracleDbType.Int32, ParameterDirection.Output);
cmd.Parameters.Add("x_return_code", OracleDbType.Varchar2, ParameterDirection.Output);
cmd.Parameters.Add("x_return_msg", OracleDbType.Varchar2, ParameterDirection.Output);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
string str = exp.StackTrace.ToString();
string msg = exp.Message.ToString();
}
}
You have this OracleParameter:
OracleParameter P_SO_LINES_TAB_ITEM = new OracleParameter();
P_SO_LINES_TAB_ITEM.ParameterName = "p_so_lines_tab_item";
P_SO_LINES_TAB_ITEM.OracleDbType = OracleDbType.NVarchar2;
P_SO_LINES_TAB_ITEM.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
P_SO_LINES_TAB_ITEM.Size = 1000;
P_SO_LINES_TAB_ITEM.Direction = ParameterDirection.Input;
Value of P_SO_LINES_TAB_ITEM must be a list of plain text (i.e. NVARCHAR2 as you declared by property OracleDbType). However, you try to use a list of OracleParameter objects.
P_SO_LINES_TAB_ITEM.Value = new object[] { QUANTITY, QUANTITY_UOM, ITEM_ID, PROMISE_DATE, SCHEDULE_SHIP_DATE, CURRENCY_CODE, UNIT_SELLING_PRICE, SALESREP_ID };
This is not possible, you cannot make nested parameters.
Got a solution for my problem .We can pass a collection of data by setting the oracle.Dbtype as NVarchar as shown below.
var commandObject = new OracleCommand
{
Connection = con,
CommandText = "Your Packge Name",
CommandType = CommandType.StoredProcedure
};
commandObject.Connection.Open();
commandObject.Parameters.Add("param1", OracleDbType.Int32, ParameterDirection.Input).Value = some int value;
commandObject.Parameters.Add("param2", OracleDbType.Int32, ParameterDirection.Input).Value = some int value;
commandObject.Parameters.Add("param3", OracleDbType.Int32, ParameterDirection.Input).Value = some int value;
commandObject.Parameters.Add("p_xml_type", OracleDbType.NVarchar2, ParameterDirection.Input).Value =some collection
commandObject.Parameters.Add("param returncode", OracleDbType.Varchar2, 200).Direction = "some string value";
commandObject.ExecuteNonQuery();
The collection being set is in xml format of type string and in the oracle package the parameter has been defined as shown below.
p_xml_type IN CLOB
I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}
You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);
First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value
I have a definiton of procedure
public int Add_Nastavenie(out int typNastav, int nastavID, string hod)
{
ResetParameters();
cmd.CommandText = "add_Nastav";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParameter;
var sqlParameterOut = new SqlParameter("#TypNastav", SqlDbType.Int);
sqlParameterOut.Direction = ParameterDirection.Output;
sqlParameter = new SqlParameter("#NastavenieID", SqlDbType.Int);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = nastavID;
cmd.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("#Hodnota", SqlDbType.NVarChar, 100);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = hod;
cmd.Parameters.Add(sqlParameter);
var sqlParameterRet = new SqlParameter("retValue", SqlDbType.Int);
sqlParameterRet.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
typNastav = (int)sqlParameterOut.Value;
return (int)cmd.Parameters["retvalue"].Value;
}
I call this procedure like this :
dataConnector.Add_Nastavenie(out typNastav,nastavID,hod);
It's show error :
sql parameter with ParameterName 'retvalue' is not contained by this
sqlParameterCollection
How can i fix it ?
You are getting this because you haven't added the return parameter to command. you missed this
cmd.Parameters.Add(sqlParameterRet );
before
cmd.ExecuteNonQuery();