ORA-01008: not all variables bound in C# - c#

The code I wrote is as follows:
var query = #"SELECT t0.ACT_TYPE, t0.ST_DT,t0.AC_ST_DT FROM ACTIVITY t0 WHERE (t0.AC_ST_DT > :p0 OR t0.ST_DT > :p0) AND t0.ACT_TYPE = :p1";
OracleConnection connection = new OracleConnection(cs);
try
{
connection.Open();
OracleCommand cmd = new OracleCommand(query, connection);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
OracleParameter pp = new OracleParameter();
DateTime date = new DateTime(2016, 01, 01);
OracleParameter dp = new OracleParameter();
dp.OracleDbType = OracleDbType.Date;
dp.ParameterName = "p0";
dp.Value = date;
cmd.Parameters.Add(dp);
var sp = new OracleParameter();
sp.OracleDbType = OracleDbType.Varchar2;
sp.ParameterName = "p1";
sp.Value = "SC";
cmd.Parameters.Add(sp);
var dt = new DataTable();
oda.Fill(dt);
cmd.Dispose();
}
finally
{
connection.Close();
}
Error I got is like
ORA-01008: not all variables bound
Can someone help me out on this problem.

I don't think it's possible to reuse positional parameters in this way. Instead use three bona fide parameters and then bind them:
var query = #"SELECT t0.ACT_TYPE, t0.ST_DT,t0.AC_ST_DT FROM ACTIVITY t0 WHERE (t0.AC_ST_DT > :p0 OR t0.ST_DT > :p1) AND t0.ACT_TYPE = :p2";
OracleConnection connection = new OracleConnection(cs);
try
{
connection.Open();
OracleCommand cmd = new OracleCommand(query, connection);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
OracleParameter pp = new OracleParameter();
DateTime date = new DateTime(2016, 01, 01);
OracleParameter dp1 = new OracleParameter();
dp1.OracleDbType = OracleDbType.Date;
dp1.ParameterName = "p0";
dp1.Value = date;
cmd.Parameters.Add(dp1);
OracleParameter dp2 = new OracleParameter();
dp2.OracleDbType = OracleDbType.Date;
dp2.ParameterName = "p1";
dp2.Value = date;
cmd.Parameters.Add(dp2);
var sp = new OracleParameter();
sp.OracleDbType = OracleDbType.Varchar2;
sp.ParameterName = "p2";
sp.Value = "SC";
cmd.Parameters.Add(sp);
var dt = new DataTable();
oda.Fill(dt);
cmd.Dispose();
}
finally
{
connection.Close();
}

Related

DataReader is not getting values

DateTime dtExpiry = DateTime.Now.AddDays(1);
CommonFunctionDAL objCommDAL = new CommonFunctionDAL();
SqlCommand cmd = new SqlCommand("sp_LoginCheck", sqlconnection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#UserName", emailid);
SqlParameter p2 = new SqlParameter("#Password", password);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
SqlParameter sEMPID = cmd.Parameters.Add("#status", SqlDbType.Int, 5);
sEMPID.Direction = ParameterDirection.Output;
sqlconnection.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
HttpCookie AppCookies = new HttpCookie("AppCookies");
AppCookies["EMPID"] = rd["EMPID"].ToString();
AppCookies["AEMPID"] = rd["AEMPID"].ToString();
AppCookies["EmpName"] = rd["EmpName"].ToString();
AppCookies["EmpDepartment"] = rd["EmpDepartment"].ToString();
AppCookies.Expires = dtExpiry;
HttpContext.Current.Response.Cookies.Add(AppCookies);
int Result = cmd.ExecuteNonQuery();
return sEMPID.Value.ToString();
}
I am getting enumeration yielded no results.

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

SQL Command Parameters

I have an asp.net MVC web app that adds data to a sql database. It has been working fine with the following code:
query = "insert into ImageNameTable(ImageName,NotifyDate,NotifyDate2) values(#ImageName,#NotifyDate,#NotifyDate2)";
SqlParameter StringParameter = new SqlParameter();
StringParameter.SqlDbType = SqlDbType.VarChar;
StringParameter.ParameterName = "#ImageName";
StringParameter.Value = imageName;
DateTime now = DateTime.Now;
now = now.AddMilliseconds(-now.Millisecond);
DateTime notifyDate = now.AddDays(1);
DateTime notify = DateTime.Now;
DateTime aNotify = DateTime.Now;
notify = notify.AddMilliseconds(-notify.Millisecond);
notify = notify.AddMinutes(1);
aNotify = notify.AddMinutes(1);
SqlParameter notifyDateParameter = new SqlParameter();
notifyDateParameter.SqlDbType = SqlDbType.DateTime;
notifyDateParameter.ParameterName = "#NotifyDate";
notifyDateParameter.Value = notify;
SqlParameter notifyDate2Parameter = new SqlParameter();
notifyDate2Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate2Parameter.ParameterName = "#NotifyDate2";
notifyDate2Parameter.Value = aNotify;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(StringParameter);
cmd.Parameters.Add(notifyDateParameter);
cmd.Parameters.Add(notifyDate2Parameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
This was used for testing purposes. So now I have changed it to what I actually want it to do, and nothing is appearing in the sql database. Essentially, all I have done is added parameters to the sql command. There is no error, just that it doesn't appear in the database. Here is the change:
query = "insert into ImageNameTable(ImageName,NotifyDate,NotifyDate2,NotifyDate3,NotifyDate4,NotifyDate5,NotifyDate6,NotifyDate7,NotifyDate8,NotifyDate9,NotifyDate10,NotifyDate11,NotifyDate12,NotifyDate13,NotifyDate14) values(#ImageName,#NotifyDate,#NotifyDate2,#NotifyDate3,#NotifyDate4,#NotifyDate5,#NotifyDate6,#NotifyDate7,#NotifyDate8,#NotifyDate9,#NotifyDate10,#NotifyDate11,#NotifyDate12,#NotifyDate13,#NotifyDate14)";
SqlParameter StringParameter = new SqlParameter();
StringParameter.SqlDbType = SqlDbType.VarChar;
StringParameter.ParameterName = "#ImageName";
StringParameter.Value = imageName;
DateTime now = DateTime.Now;
now = now.AddMilliseconds(-now.Millisecond);
DateTime notifyDate = now.AddDays(1);
DateTime notify = DateTime.Now;
DateTime aNotify = DateTime.Now;
notify = notify.AddMilliseconds(-notify.Millisecond);
notify = notify.AddHours(1);
aNotify = notify.AddHours(5);
DateTime bNotify = aNotify.AddDays(1);
DateTime cNotify = bNotify.AddDays(1);
DateTime dNotify = cNotify.AddDays(2);
DateTime eNotify = dNotify.AddDays(2);
DateTime fNotify = eNotify.AddDays(3);
DateTime gNotify = fNotify.AddDays(4);
DateTime hNotify = gNotify.AddDays(7);
DateTime iNotify = hNotify.AddDays(14);
DateTime jNotify = bNotify.AddMonths(2);
DateTime kNotify = bNotify.AddMonths(3);
DateTime lNotify = bNotify.AddMonths(4);
DateTime mNotify = bNotify.AddMonths(6);
SqlParameter notifyDateParameter = new SqlParameter();
notifyDateParameter.SqlDbType = SqlDbType.DateTime;
notifyDateParameter.ParameterName = "#NotifyDate";
notifyDateParameter.Value = notify;
SqlParameter notifyDate2Parameter = new SqlParameter();
notifyDate2Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate2Parameter.ParameterName = "#NotifyDate2";
notifyDate2Parameter.Value = aNotify;
SqlParameter notifyDate3Parameter = new SqlParameter();
notifyDate3Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate3Parameter.ParameterName = "#NotifyDate3";
notifyDate3Parameter.Value = bNotify;
SqlParameter notifyDate4Parameter = new SqlParameter();
notifyDate4Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate4Parameter.ParameterName = "#NotifyDate4";
notifyDate4Parameter.Value = cNotify;
SqlParameter notifyDate5Parameter = new SqlParameter();
notifyDate5Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate5Parameter.ParameterName = "#NotifyDate5";
notifyDate5Parameter.Value = dNotify;
SqlParameter notifyDate6Parameter = new SqlParameter();
notifyDate6Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate6Parameter.ParameterName = "#NotifyDate6";
notifyDate6Parameter.Value = eNotify;
SqlParameter notifyDate7Parameter = new SqlParameter();
notifyDate7Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate7Parameter.ParameterName = "#NotifyDate7";
notifyDate7Parameter.Value = fNotify;
SqlParameter notifyDate8Parameter = new SqlParameter();
notifyDate8Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate8Parameter.ParameterName = "#NotifyDate8";
notifyDate8Parameter.Value = gNotify;
SqlParameter notifyDate9Parameter = new SqlParameter();
notifyDate9Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate9Parameter.ParameterName = "#NotifyDate9";
notifyDate9Parameter.Value = hNotify;
SqlParameter notifyDate10Parameter = new SqlParameter();
notifyDate10Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate10Parameter.ParameterName = "#NotifyDate10";
notifyDate10Parameter.Value = iNotify;
SqlParameter notifyDate11Parameter = new SqlParameter();
notifyDate11Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate11Parameter.ParameterName = "#NotifyDate11";
notifyDate11Parameter.Value = jNotify;
SqlParameter notifyDate12Parameter = new SqlParameter();
notifyDate12Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate12Parameter.ParameterName = "#NotifyDate12";
notifyDate12Parameter.Value = kNotify;
SqlParameter notifyDate13Parameter = new SqlParameter();
notifyDate13Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate3Parameter.ParameterName = "#NotifyDate13";
notifyDate13Parameter.Value = lNotify;
SqlParameter notifyDate14Parameter = new SqlParameter();
notifyDate14Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate14Parameter.ParameterName = "#NotifyDate14";
notifyDate14Parameter.Value = mNotify;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(StringParameter);
cmd.Parameters.Add(notifyDateParameter);
cmd.Parameters.Add(notifyDate2Parameter);
cmd.Parameters.Add(notifyDate3Parameter);
cmd.Parameters.Add(notifyDate4Parameter);
cmd.Parameters.Add(notifyDate5Parameter);
cmd.Parameters.Add(notifyDate6Parameter);
cmd.Parameters.Add(notifyDate7Parameter);
cmd.Parameters.Add(notifyDate8Parameter);
cmd.Parameters.Add(notifyDate9Parameter);
cmd.Parameters.Add(notifyDate10Parameter);
cmd.Parameters.Add(notifyDate11Parameter);
cmd.Parameters.Add(notifyDate12Parameter);
cmd.Parameters.Add(notifyDate13Parameter);
cmd.Parameters.Add(notifyDate14Parameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
I'm wondering if I have used too many parameters, but I've been told that the max amount of parameters a command can hold is way more than 14. Does anyone know what is going on here?
notifyDate3Parameter.ParameterName = "#NotifyDate13"; - note 3 and 13 to the left and to the right of the assignment sign, respectively.

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

Categories