Out parameters in stored procedures (oracle database) - c#

I used stored proceture in the Oracle. How can I use out parameteres of the SP in C# code?
I use the following code for it:
OracleSP
PROCEDURE TABMPWORKREQUEST_INS(INTWORKREQUEST_ IN NUMBER, VCWORKREQUESTNUMBER_ OUT VARCHAR2,
INTREQUESTEDBY_ IN NUMBER, INTWORKTYPE_ IN NUMBER,INTACTIONTYPE_ IN NUMBER,
INTPRIORITY_ IN NUMBER, INTRECORDID_ IN NUMBER, INTPERMITREQUIRED_ IN NUMBER, VCINSPECTIONREQUIRED_ IN CHAR,
VCLASTUSERID_ IN VARCHAR2, INTCOSTCENTRECODE_ IN NUMBER, VCBUDGETHEAD_ IN VARCHAR2
, VCREFERENCEWORKORDERNUMBER_ IN VARCHAR2, VCJOBDESCRIPTION_ IN VARCHAR2,
VCSTATUS_ IN CHAR, VCHISTORYFLAG_ IN CHAR, VCREASONCODE_ IN VARCHAR2
, VCMSSNUMBER_ IN VARCHAR2, INTAUTHORIZELEVEL_ IN NUMBER, INTPRINTFLAG_ IN NUMBER,
INTINSPECTIONFLAG_ IN NUMBER, INTDAYNUMBER_ IN NUMBER, INTDURATION_ IN NUMBER, INTPROGRESS_ IN NUMBER,
INTWORKSHOPFLAG_ IN CHAR)
IS
EXISTANCE number;
UID_ VARCHAR(5);
SS varchar2(20);
BEGIN
SELECT COUNT(*) into EXISTANCE FROM TABMPWORKREQUEST WHERE INTWORKREQUEST = INTWORKREQUEST_ ;
IF VCLASTUSERID_ = '0' THEN
UID_ := 'U2';
ELSE
UID_ := VCLASTUSERID_;
END IF;
select GetWorkOrderNumber(INTREQUESTEDBY_) INTO SS from dual ;
VCWORKREQUESTNUMBER_ :=SS;
INSERT INTO TABMPWORKREQUEST
VALUES( CMMS.SEQTABMPWORKREQUEST.NEXTVAL , SS , sysdate , INTrequestedby_ ,
INTworktype_ , INTactiontype_ , INTpriority_ , Intrecordid_ , Intpermitrequired_ , Vcinspectionrequired_ ,
Vclastuserid_ , SYSDATE , sysdate , INTcostcentrecode_ , Vcbudgethead_ , sysdate ,
Vcreferenceworkordernumber_ , Vcjobdescription_ , '01' , Vchistoryflag_ , Vcreasoncode_ ,
sysdate , sysdate , Vcmssnumber_ , Intauthorizelevel_ , Intprintflag_ , Intinspectionflag_ ,
Intdaynumber_ , Intduration_ , Intprogress_ , Intworkshopflag_ );
END TABMPWORKREQUEST_INS;
and c# code
public bool InsertMpWORKREQUEST(ClsFieldsMpWORKREQUEST fieldsMpWORKREQUEST)
{
string DTBDDATETIME;
if (fieldsMpWORKREQUEST.DTBDDATETIME != null)
{
DTBDDATETIME = String.Format("{0:dd/MM/yyyy HH:mm:ss}", fieldsMpWORKREQUEST.DTBDDATETIME);
DTBDDATETIME = "TO_DATE('" + DTBDDATETIME + "', 'MM/DD/YYYY HH24:MI:SS')";
}
else
{
DTBDDATETIME = "NULL";
}
string DateExpire = string.Empty;
if (fieldsMpWORKREQUEST.DTEXPIRYDATE != null)
{
DateExpire = String.Format("{0:dd/MM/yyyy HH:mm:ss}", fieldsMpWORKREQUEST.DTEXPIRYDATE);
DateExpire = "TO_DATE('" + DateExpire + "', 'MM/DD/YYYY HH24:MI:SS')";
}
else
{
DateExpire = "NULL";
}
string DtStartDate;
if (fieldsMpWORKREQUEST.DTSTARTDATE != null)
{
DtStartDate = String.Format("{0:dd/MM/yyyy HH:mm:ss}", fieldsMpWORKREQUEST.DTSTARTDATE);
DtStartDate = "TO_DATE('" + DtStartDate + "', 'MM/DD/YYYY HH24:MI:SS')";
}
else
{
DtStartDate = "NULL";
}
string DtComplementationDate;
if (fieldsMpWORKREQUEST.DTCOMPLETIONDATE != null)
{
DtComplementationDate = String.Format("{0:dd/MM/yyyy HH:mm:ss}", fieldsMpWORKREQUEST.DTCOMPLETIONDATE);
DtComplementationDate = "TO_DATE('" + DtComplementationDate + "', 'MM/DD/YYYY HH24:MI:SS')";
}
else
{
DtComplementationDate = "NULL";
}
OracleConnection cn = new OracleConnection(this.GetConnectionString());
OracleCommand cmd = new OracleCommand("CMMS_MPWORKREQUEST_PKG.TABMPWORKREQUEST_INS", cn);
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter INTWORKREQUEST = new OracleParameter();
INTWORKREQUEST.ParameterName = "INTWORKREQUEST_";
INTWORKREQUEST.OracleType = OracleType.Number;
INTWORKREQUEST.Direction = ParameterDirection.Input;
INTWORKREQUEST.Value = fieldsMpWORKREQUEST.INTWORKREQUEST;
cmd.Parameters.Add(INTWORKREQUEST);
// cmd.Parameters.AddWithValue("VCWORKREQUESTNUMBER_", fieldsMpWORKREQUEST.VCWORKREQUESTNUMBER).Direction=ParameterDirection.Output;
OracleParameter requestNo = new OracleParameter();
requestNo.ParameterName = "VCWORKREQUESTNUMBER_";
requestNo.OracleType = OracleType.VarChar;
requestNo.Size = 14;
requestNo.Direction = ParameterDirection.Output;
cmd.Parameters.Add(requestNo);
OracleParameter INTREQUESTEDBY = new OracleParameter();
INTREQUESTEDBY.ParameterName = "INTREQUESTEDBY_";
INTREQUESTEDBY.OracleType = OracleType.Number;
INTREQUESTEDBY.Direction = ParameterDirection.Input;
INTREQUESTEDBY.Value = fieldsMpWORKREQUEST.INTREQUESTEDBY.INTSECTIONID;
cmd.Parameters.Add(INTREQUESTEDBY);
OracleParameter INTWORKTYPE = new OracleParameter();
INTWORKTYPE.ParameterName = "INTWORKTYPE_";
INTWORKTYPE.OracleType = OracleType.Number;
INTWORKTYPE.Direction = ParameterDirection.Input;
INTWORKTYPE.Value = fieldsMpWORKREQUEST.INTREQUESTEDBY.INTSECTIONID;
cmd.Parameters.Add(INTWORKTYPE);
OracleParameter INTACTIONTYPE = new OracleParameter();
INTACTIONTYPE.ParameterName = "INTACTIONTYPE_";
INTACTIONTYPE.OracleType = OracleType.Number;
INTACTIONTYPE.Direction = ParameterDirection.Input;
INTACTIONTYPE.Value = fieldsMpWORKREQUEST.INTACTIONTYPE.INTSECTIONID;
cmd.Parameters.Add(INTACTIONTYPE);
OracleParameter INTPRIORITY = new OracleParameter();
INTPRIORITY.ParameterName = "INTPRIORITY_";
INTPRIORITY.OracleType = OracleType.Number;
INTPRIORITY.Direction = ParameterDirection.Input;
INTPRIORITY.Value = fieldsMpWORKREQUEST.INTPRIORITY.INTGENERALCODE;
cmd.Parameters.Add(INTPRIORITY);
OracleParameter INTRECORDID = new OracleParameter();
INTRECORDID.ParameterName = "INTRECORDID_";
INTRECORDID.OracleType = OracleType.Number;
INTRECORDID.Direction = ParameterDirection.Input;
INTRECORDID.Value = fieldsMpWORKREQUEST.INTRECORDID;
cmd.Parameters.Add(INTRECORDID);
OracleParameter INTPERMITREQUIRED = new OracleParameter();
INTPERMITREQUIRED.ParameterName = "INTPERMITREQUIRED_";
INTPERMITREQUIRED.OracleType = OracleType.Number;
INTPERMITREQUIRED.Direction = ParameterDirection.Input;
INTPERMITREQUIRED.Value = fieldsMpWORKREQUEST.INTPERMITREQUIRED;
cmd.Parameters.Add(INTPERMITREQUIRED);
OracleParameter VCINSPECTIONREQUIRED = new OracleParameter();
VCINSPECTIONREQUIRED.ParameterName = "VCINSPECTIONREQUIRED_";
VCINSPECTIONREQUIRED.OracleType = OracleType.Char;
VCINSPECTIONREQUIRED.Direction = ParameterDirection.Input;
VCINSPECTIONREQUIRED.Value = fieldsMpWORKREQUEST.VCINSPECTIONREQUIRED;
cmd.Parameters.Add(VCINSPECTIONREQUIRED);
OracleParameter VCLASTUSERID = new OracleParameter();
VCLASTUSERID.ParameterName = "VCLASTUSERID_";
VCLASTUSERID.OracleType = OracleType.VarChar;
VCLASTUSERID.Direction = ParameterDirection.Input;
VCLASTUSERID.Value = fieldsMpWORKREQUEST.VCLASTUSERID;
cmd.Parameters.Add(VCLASTUSERID);
OracleParameter INTCOSTCENTRECODE = new OracleParameter();
INTCOSTCENTRECODE.ParameterName = "INTCOSTCENTRECODE_";
INTCOSTCENTRECODE.OracleType = OracleType.Number;
INTCOSTCENTRECODE.Direction = ParameterDirection.Input;
INTCOSTCENTRECODE.Value = fieldsMpWORKREQUEST.INTCOSTCENTRECODE.INTGENERALCODE;
cmd.Parameters.Add(INTCOSTCENTRECODE);
OracleParameter vCBUDGETHEAD = new OracleParameter();
vCBUDGETHEAD.ParameterName = "VCBUDGETHEAD_";
vCBUDGETHEAD.OracleType = OracleType.VarChar;
vCBUDGETHEAD.Direction = ParameterDirection.Input;
vCBUDGETHEAD.Value = fieldsMpWORKREQUEST.VCBUDGETHEAD;
cmd.Parameters.Add(vCBUDGETHEAD);
OracleParameter VCREFERENCEWORKORDERNUMBER = new OracleParameter();
VCREFERENCEWORKORDERNUMBER.ParameterName = "VCREFERENCEWORKORDERNUMBER_";
VCREFERENCEWORKORDERNUMBER.OracleType = OracleType.VarChar;
VCREFERENCEWORKORDERNUMBER.Direction = ParameterDirection.Input;
VCREFERENCEWORKORDERNUMBER.Value = fieldsMpWORKREQUEST.VCREFERENCEWORKORDERNUMBER;
cmd.Parameters.Add(VCREFERENCEWORKORDERNUMBER);
OracleParameter VCJOBDESCRIPTION = new OracleParameter();
VCJOBDESCRIPTION.ParameterName = "VCJOBDESCRIPTION_";
VCJOBDESCRIPTION.OracleType = OracleType.VarChar;
VCJOBDESCRIPTION.Direction = ParameterDirection.Input;
VCJOBDESCRIPTION.Value = fieldsMpWORKREQUEST.VCJOBDESCRIPTION;
cmd.Parameters.Add(VCJOBDESCRIPTION);
OracleParameter VCSTATUS = new OracleParameter();
VCSTATUS.ParameterName = "VCSTATUS_";
VCSTATUS.OracleType = OracleType.Char;
VCSTATUS.Direction = ParameterDirection.Input;
VCSTATUS.Value = fieldsMpWORKREQUEST.VCSTATUS;
cmd.Parameters.Add(VCSTATUS);
OracleParameter VCHISTORYFLAG = new OracleParameter();
VCHISTORYFLAG.ParameterName = "VCHISTORYFLAG_";
VCHISTORYFLAG.OracleType = OracleType.Char;
VCHISTORYFLAG.Direction = ParameterDirection.Input;
VCHISTORYFLAG.Value = fieldsMpWORKREQUEST.VCHISTORYFLAG;
cmd.Parameters.Add(VCHISTORYFLAG);
OracleParameter VCREASONCODE = new OracleParameter();
VCREASONCODE.ParameterName = "VCREASONCODE_";
VCREASONCODE.OracleType = OracleType.VarChar;
VCREASONCODE.Direction = ParameterDirection.Input;
VCREASONCODE.Value = fieldsMpWORKREQUEST.VCREASONCODE;
cmd.Parameters.Add(VCREASONCODE);
OracleParameter VCMSSNUMBER = new OracleParameter();
VCMSSNUMBER.ParameterName = "VCMSSNUMBER_";
VCMSSNUMBER.OracleType = OracleType.VarChar;
VCMSSNUMBER.Direction = ParameterDirection.Input;
VCMSSNUMBER.Value = fieldsMpWORKREQUEST.VCMSSNUMBER;
cmd.Parameters.Add(VCMSSNUMBER);
OracleParameter INTAUTHORIZELEVEL = new OracleParameter();
INTAUTHORIZELEVEL.ParameterName = "INTAUTHORIZELEVEL_";
INTAUTHORIZELEVEL.OracleType = OracleType.Number;
INTAUTHORIZELEVEL.Direction = ParameterDirection.Input;
INTAUTHORIZELEVEL.Value = fieldsMpWORKREQUEST.INTAUTHORIZELEVEL;
cmd.Parameters.Add(INTAUTHORIZELEVEL);
OracleParameter INTPRINTFLAG = new OracleParameter();
INTPRINTFLAG.ParameterName = "INTPRINTFLAG_";
INTPRINTFLAG.OracleType = OracleType.Number;
INTPRINTFLAG.Direction = ParameterDirection.Input;
INTPRINTFLAG.Value = fieldsMpWORKREQUEST.INTPRINTFLAG;
cmd.Parameters.Add(INTPRINTFLAG);
OracleParameter INTINSPECTIONFLAG = new OracleParameter();
INTINSPECTIONFLAG.ParameterName = "INTINSPECTIONFLAG_";
INTINSPECTIONFLAG.OracleType = OracleType.Number;
INTINSPECTIONFLAG.Direction = ParameterDirection.Input;
INTINSPECTIONFLAG.Value = fieldsMpWORKREQUEST.INTINSPECTIONFLAG;
cmd.Parameters.Add(INTINSPECTIONFLAG);
OracleParameter INTDAYNUMBER = new OracleParameter();
INTDAYNUMBER.ParameterName = "INTDAYNUMBER_";
INTDAYNUMBER.OracleType = OracleType.Number;
INTDAYNUMBER.Direction = ParameterDirection.Input;
INTDAYNUMBER.Value = fieldsMpWORKREQUEST.INTDAYNUMBER;
cmd.Parameters.Add(INTDAYNUMBER);
OracleParameter INTDURATION = new OracleParameter();
INTDURATION.ParameterName = "INTDURATION_";
INTDURATION.OracleType = OracleType.Number;
INTDURATION.Direction = ParameterDirection.Input;
INTDURATION.Value = fieldsMpWORKREQUEST.INTDURATION;
cmd.Parameters.Add(INTDURATION);
OracleParameter INTPROGRESS = new OracleParameter();
INTPROGRESS.ParameterName = "INTPROGRESS_";
INTPROGRESS.OracleType = OracleType.Number;
INTPROGRESS.Direction = ParameterDirection.Input;
INTPROGRESS.Value = fieldsMpWORKREQUEST.INTPROGRESS;
cmd.Parameters.Add(INTPROGRESS);
OracleParameter INTWORKSHOPFLAG = new OracleParameter();
INTWORKSHOPFLAG.ParameterName = "INTWORKSHOPFLAG_";
INTWORKSHOPFLAG.OracleType = OracleType.Char;
INTWORKSHOPFLAG.Direction = ParameterDirection.Input;
INTWORKSHOPFLAG.Value = fieldsMpWORKREQUEST.INTWORKSHOPFLAG;
cmd.Parameters.Add(INTWORKSHOPFLAG);
string requestNo2 = string.Empty;
int rowEfect = 0;
try
{
cn.Open();
rowEfect = cmd.ExecuteNonQuery();
requestNo2 = cmd.Parameters["VCWORKREQUESTNUMBER_"].Value.ToString();
}
catch (Exception)
{
throw;
}
finally
{
if (cn.State != ConnectionState.Closed) cn.Close();
}
return rowEfect > 0;
}
But I get the following error:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'TABMPWORKREQUEST_INS'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
How can I fix this problem?

The objects in the MSFT System.Data namespace relating to Oracle have been deprecated:
http://msdn.microsoft.com/en-us/library/77d8yct7.aspx
But there is a generic System.Data.OleDB namespace:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.direction(v=VS.71).aspx
The Command object has a parameters collection. Each parameter has several properties, including a direction property. When you set that property to output, you can examine its value after issuing the command.
A third-party provider for Oracle should offer something similar.
P.S. I haven't used the OleDB namespace in a very long time -- but recall that the parameters had to be added to the Parameters collection in the same order in which they appear in the SP's signature. Be sure to set the appropriate datatype for each parameter and provide the length of any text values being passed.
P.P.S. It looks as though you're adding the same parameter to the collection twice; is one of these supposed to be commented out?
cmd.Parameters.AddWithValue("VCWORKREQUESTNUMBER_",fieldsMpWORKREQUEST.VCWORKREQUESTNUMBER).Direction=ParameterDirection.Output;
OracleParameter requestNo = new OracleParameter();
requestNo.ParameterName = "VCWORKREQUESTNUMBER_";
requestNo.OracleType = OracleType.VarChar;
requestNo.Size = 14;
requestNo.Direction = ParameterDirection.Output;
cmd.Parameters.Add(requestNo);

Related

How to execute function using oracle C# windows application

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;

Executing oracle package with recordtype input param throws exception in .NET

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

Calling an Oracle insert stored procedure from C#

Stored procedure:
CREATE OR REPLACE PROCEDURE insertMovie(
p_m_id IN MOVIE.M_ID%TYPE,
p_movie_name IN MOVIE.MOVIE_NAME%TYPE,
p_year IN MOVIE.YEAR%TYPE,
p_category IN MOVIE.CATEGORY%TYPE)
IS
BEGIN
INSERT INTO MOVIE ("M_ID", "MOVIE_NAME", "YEAR", "CATEGORY")
VALUES (p_m_id, p_movie_name,p_year, p_category);
END;
/
This is the procedure I have created to insert data into the movie table. Now I am trying to call it from by C#. I have tried the following code but it's not working.
A little help in this regard will be much appreciated. Thank you
private void button1_Click(object sender, EventArgs e)
{
dbConnection conObj = new dbConnection();
OracleConnection theConn = conObj.connFunc();
String p_m_id, p_movie_name, p_year, p_category;
p_m_id = movie_id.Text;
p_movie_name = movie_name.Text;
p_year = year.Text;
p_category = category.Text;
OracleParameter parChoiceIn = new OracleParameter();
OracleParameter parDataIn = new OracleParameter();
OracleParameter parOut = new OracleParameter();
parChoiceIn.ParameterName = "p_m_id";
parChoiceIn.OracleType = OracleType.Number;
parChoiceIn.Size = 32;
parChoiceIn.Direction = System.Data.ParameterDirection.Input;
parChoiceIn.Value = p_m_id;
parDataIn.ParameterName = "p_movie_name";
parDataIn.OracleType = OracleType.VarChar;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_movie_name;
parDataIn.ParameterName = "p_year";
parDataIn.OracleType = OracleType.Number;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_year;
parDataIn.ParameterName = "p_category";
parDataIn.OracleType = OracleType.VarChar;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_category;
OracleCommand cmd = theConn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "insertMovie";
cmd.Parameters.Add(parChoiceIn);
cmd.Parameters.Add(parDataIn);
cmd.Parameters.Add(parOut);
theConn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataSet myset = new DataSet("theResCur");
adapter.Fill(myset);
theGrid.DataSource = myset;
theGrid.DataSource = myset.Tables[0];
theConn.Close();
}
}
You are creating a parOut parameter but never initializing it.
Also, you are redefining the parDataIn parameter three times, doing so, your OracleCommand only receives the last defined parameter p_category.
I do not understand what is your intent in the last part of your code. If you want to simply call the stored procedure, maybe you should try this:
private void button1_Click(object sender, EventArgs e)
{
dbConnection conObj = new dbConnection();
OracleConnection theConn = conObj.connFunc();
String p_m_id, p_movie_name, p_year, p_category;
p_m_id = movie_id.Text;
p_movie_name = movie_name.Text;
p_year = year.Text;
p_category = category.Text;
theConn.Open();
OracleCommand cmd = theConn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "insertMovie";
cmd.Connection = theConn;
OracleParameter parChoiceIn = new OracleParameter();
OracleParameter parDataIn = new OracleParameter();
parChoiceIn.ParameterName = "p_m_id";
parChoiceIn.OracleType = OracleType.Number;
parChoiceIn.Size = 32;
parChoiceIn.Direction = System.Data.ParameterDirection.Input;
parChoiceIn.Value = p_m_id;
cmd.Parameters.Add(parChoiceIn);
parDataIn.ParameterName = "p_movie_name";
parDataIn.OracleType = OracleType.VarChar;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_movie_name;
cmd.Parameters.Add(parDataIn);
parDataIn = new OracleParameter();
parDataIn.ParameterName = "p_year";
parDataIn.OracleType = OracleType.Number;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_year;
cmd.Parameters.Add(parDataIn);
parDataIn = new OracleParameter();
parDataIn.ParameterName = "p_category";
parDataIn.OracleType = OracleType.VarChar;
parDataIn.Size = 32;
parDataIn.Direction = System.Data.ParameterDirection.Input;
parDataIn.Value = p_category;
cmd.Parameters.Add(parDataIn);
cmd.ExecuteNonQuery();
theConn.Close();
}

Unable to get Oracle Package function to execute in c#

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 :-(

The SqlParameter is already contained by another SqlParameterCollection - command.Parameters.Clear() does not work

I am getting desperate here. I cant figure out why does this exception occur. I searched and read about this problem and it seems everyone managed to solve it with command.Parameters.Clear(), but this does not work for me. Here is my code:
public class ProductsDataAccess : Connection
{
public static SqlParameter[] createParams(Products prod)
{
SqlParameter idParam = new SqlParameter("#id", prod.prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter nameParam = new SqlParameter("#name", prod.prodName);
nameParam.SqlDbType = System.Data.SqlDbType.VarChar;
nameParam.Size = 45;
nameParam.IsNullable = false;
nameParam.Direction = ParameterDirection.Input;
SqlParameter demandParam = new SqlParameter("#demand", prod.demand);
demandParam.SqlDbType = System.Data.SqlDbType.Int;
demandParam.IsNullable = false;
demandParam.Direction = ParameterDirection.Input;
SqlParameter demandLTParam = new SqlParameter("#demandLT", prod.demandLT);
demandLTParam.SqlDbType = System.Data.SqlDbType.Int;
demandLTParam.IsNullable = false;
demandLTParam.Direction = ParameterDirection.Input;
SqlParameter leadTimeParam = new SqlParameter("#leadTime", prod.leadTime);
leadTimeParam.SqlDbType = System.Data.SqlDbType.Int;
leadTimeParam.IsNullable = false;
leadTimeParam.Direction = ParameterDirection.Input;
SqlParameter fixedCoastParam = new SqlParameter("#k", prod.k);
fixedCoastParam.SqlDbType = System.Data.SqlDbType.Float;
fixedCoastParam.IsNullable = false;
fixedCoastParam.Direction = ParameterDirection.Input;
SqlParameter cParam = new SqlParameter("#c", prod.c);
cParam.SqlDbType = System.Data.SqlDbType.Float;
cParam.IsNullable = false;
cParam.Direction = ParameterDirection.Input;
SqlParameter chPrecentageParam = new SqlParameter("#chPrecent", prod.precentOfC);
chPrecentageParam.SqlDbType = System.Data.SqlDbType.Int;
chPrecentageParam.IsNullable = false;
chPrecentageParam.Direction = ParameterDirection.Input;
SqlParameter chParam = new SqlParameter("#ch", prod.ch);
chParam.SqlDbType = System.Data.SqlDbType.Float;
chParam.IsNullable = false;
chParam.Direction = ParameterDirection.Input;
SqlParameter optimalQParam = new SqlParameter("#q", prod.q);
optimalQParam.SqlDbType = System.Data.SqlDbType.Int;
optimalQParam.IsNullable = false;
optimalQParam.Direction = ParameterDirection.Input;
SqlParameter annualChParam = new SqlParameter("#annualCh", prod.annualCh);
annualChParam.SqlDbType = System.Data.SqlDbType.Float;
annualChParam.IsNullable = false;
annualChParam.Direction = ParameterDirection.Input;
SqlParameter annualKParam = new SqlParameter("#annualK", prod.annualK);
annualKParam.SqlDbType = System.Data.SqlDbType.Float;
annualKParam.IsNullable = false;
annualKParam.Direction = ParameterDirection.Input;
SqlParameter optimalTACParam = new SqlParameter("#tac", prod.tac);
optimalTACParam.SqlDbType = System.Data.SqlDbType.Float;
optimalTACParam.IsNullable = false;
optimalTACParam.Direction = ParameterDirection.Input;
SqlParameter rParam = new SqlParameter("#r", prod.r);
rParam.SqlDbType = System.Data.SqlDbType.Int;
rParam.IsNullable = false;
rParam.Direction = ParameterDirection.Input;
SqlParameter inventoryLvlParam = new SqlParameter("#invLvl", prod.inventoryLvl);
inventoryLvlParam.SqlDbType = System.Data.SqlDbType.Int;
inventoryLvlParam.IsNullable = false;
inventoryLvlParam.Direction = ParameterDirection.Input;
SqlParameter statusParam = new SqlParameter("#status", prod.status);
statusParam.SqlDbType = System.Data.SqlDbType.VarChar;
statusParam.Size = 10;
statusParam.IsNullable = false;
statusParam.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, nameParam, demandParam, demandLTParam,
leadTimeParam, fixedCoastParam, cParam,
chPrecentageParam, chParam, optimalQParam,
annualChParam, annualKParam, optimalTACParam,
rParam, inventoryLvlParam, statusParam };
return paramList;
}
public static void insertProduct(Products product)
{
String comStr = "insert into Products(prodID, name, demand, demandLT, leadTime, fixedCoast, " +
"c, chAsPrecentage, ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus) " +
"values(#id, #name, #demand, #demandLT, #leadTime, #k, #c, #chPrecent, " +
"#ch, #q, #annualCh, #annualK, #tac, #r, #invLvl, #status)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(product));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear()
command.Dispose();
}
public static DataTable getAllProducts()
{
String comStr = "select prodID, name, demand, demandLT, leadTime, fixedCoast, c, chAsPrecentage, " +
"ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus from Products";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
DataTable table = new DataTable("allProduct");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand=command;
openConnection();
adapter.Fill(table);
command.Dispose();
closeConnection();
return table;
}
}
Here is the other class in which I am using sqlParameters:
public class StatisticsDataAccess : Connection
{
public static SqlParameter[] createParams(Statistics stat, string prodID)
{
SqlParameter idParam = new SqlParameter("#id", prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter m1Param = new SqlParameter("#m1", stat.month1);
m1Param.SqlDbType = System.Data.SqlDbType.Int;
m1Param.IsNullable = false;
m1Param.Direction = ParameterDirection.Input;
SqlParameter m2Param = new SqlParameter("#m2", stat.month2);
m2Param.SqlDbType = System.Data.SqlDbType.Int;
m2Param.IsNullable = false;
m2Param.Direction = ParameterDirection.Input;
SqlParameter m3Param = new SqlParameter("#m3", stat.month3);
m3Param.SqlDbType = System.Data.SqlDbType.Int;
m3Param.IsNullable = false;
m3Param.Direction = ParameterDirection.Input;
SqlParameter m4Param = new SqlParameter("#m4", stat.month4);
m4Param.SqlDbType = System.Data.SqlDbType.Int;
m4Param.IsNullable = false;
m4Param.Direction = ParameterDirection.Input;
SqlParameter m5Param = new SqlParameter("#m5", stat.month5);
m5Param.SqlDbType = System.Data.SqlDbType.Int;
m5Param.IsNullable = false;
m5Param.Direction = ParameterDirection.Input;
SqlParameter m6Param = new SqlParameter("#m6", stat.month6);
m6Param.SqlDbType = System.Data.SqlDbType.Int;
m6Param.IsNullable = false;
m6Param.Direction = ParameterDirection.Input;
SqlParameter m7Param = new SqlParameter("#m7", stat.month7);
m7Param.SqlDbType = System.Data.SqlDbType.Int;
m7Param.IsNullable = false;
m7Param.Direction = ParameterDirection.Input;
SqlParameter m8Param = new SqlParameter("#m8", stat.month8);
m8Param.SqlDbType = System.Data.SqlDbType.Int;
m8Param.IsNullable = false;
m8Param.Direction = ParameterDirection.Input;
SqlParameter m9Param = new SqlParameter("#m9", stat.month9);
m9Param.SqlDbType = System.Data.SqlDbType.Int;
m9Param.IsNullable = false;
m9Param.Direction = ParameterDirection.Input;
SqlParameter m10Param = new SqlParameter("#m10", stat.month10);
m10Param.SqlDbType = System.Data.SqlDbType.Int;
m10Param.IsNullable = false;
m10Param.Direction = ParameterDirection.Input;
SqlParameter m11Param = new SqlParameter("#m11", stat.month11);
m11Param.SqlDbType = System.Data.SqlDbType.Int;
m11Param.IsNullable = false;
m11Param.Direction = ParameterDirection.Input;
SqlParameter m12Param = new SqlParameter("#m12", stat.month12);
m12Param.SqlDbType = System.Data.SqlDbType.Int;
m12Param.IsNullable = false;
m12Param.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, m1Param, m2Param, m3Param, m4Param,
m5Param, m6Param, m7Param, m8Param, m8Param,
m10Param, m11Param, m12Param };
return paramList;
}
public static void insertStatistic(Statistics stat, string prodID)
{
String comStr = "insert into LTDStatistic(prodID, month1, month2, month3, month4, month5, " +
"month6, month7, month8, month9, month10, month11, month12)" +
"values(#id, #m1, #m2, #m3, #m4, #m5, #m6, #m7, #m8, #m9, #m10, #m11, #m12)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(stat, prodID));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear();
command.Dispose();
}
}
WCF Service uses methods from those classes in order to modify a data base
public void create(ProductsData product)
{
Statistics statDLT = new Statistics(product.demandLTStat.month1, product.demandLTStat.month2,
product.demandLTStat.month3, product.demandLTStat.month4, product.demandLTStat.month5,
product.demandLTStat.month6, product.demandLTStat.month7, product.demandLTStat.month8,
product.demandLTStat.month9, product.demandLTStat.month10, product.demandLTStat.month11,
product.demandLTStat.month12);
Products newProd = new Products(product.prodID, product.prodName,
product.leadTime, product.k, product.c, product.precentOfC,
product.demand, statDLT);
newProd.callculate();
newProd.determineR();
ProductsDataAccess.insertProduct(newProd);
//String plsWork = newProd.prodID;
StatisticsDataAccess.insertStatistic(newProd.demandLTStat, newProd.prodID);
}
Maybe Im missing some important aspect of how things work ... I dont know. Hope someone can help me, and sorry for the long code segments.
m8Param occurs twice in the array returned by StatisticsDataAccess.createParams().
SqlParameter[] paramList = {
idParam, m1Param, m2Param, m3Param, m4Param,
m5Param, m6Param, m7Param, **m8Param**, **m8Param**,
m10Param, m11Param, m12Param };

Categories