SQL Command Parameters - c#

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.

Related

ORA-01008: not all variables bound in 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();
}

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

C# Sql Data Reader error

I have a web form with 11 text boxes and 1 radio button list (Containing two LI's).
txtCustID - Required (Generated by getting the max CustID number from database and incrementing value)
txtFirstname - Requried
txtSurname - Required
rdbGender -> Male, Female - Required
txtAge - Required
txtAddress1 - Requried
txtAddress2 - Not required
txtCity - Required
txtPhone - Required
txtMobile - Not required
txtEmail1 - Required
txtEmail2 - Required (Used to confirm Email1, not passed to DB)
There's a button, btnAdd which passes the text values to a stored procedure "AddRecord" in the database.
It is returning an error "Procedure or function 'AddRecord' expects parameter '#Surname', which was not supplied."
I can't find the issue, as #Surname is supplied?
Here's the btnClick event code:
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pSurn.ParameterName = "#Gender";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.Int;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pSurn.ParameterName = "#Address1";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pSurn.ParameterName = "#Address2";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pSurn.ParameterName = "#City";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pSurn.ParameterName = "#Phone";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pSurn.ParameterName = "#Mobile";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pSurn.ParameterName = "#Email";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}
Here's the stored procedure code:
CREATE PROCEDURE AddRecord
#CustID INT,
#First VARCHAR,
#Surname VARCHAR,
#Gender VARCHAR,
#Age INT,
#Address1 VARCHAR,
#Address2 VARCHAR,
#City VARCHAR,
#Phone VARCHAR,
#Mobile VARCHAR,
#Email VARCHAR
AS
INSERT INTO Customer
VALUES
(#CustID, #First, #Surname, #Gender, #Age, #Address1, #Address2,
#City, #Phone, #Mobile, #Email);
You have referenced same varible after initialization of pSurn to rest of all Parameters and also give parameter name to '#Surname' for Age parameter...
Try below
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pGen.ParameterName = "#Gender";
pGen.SqlDbType = SqlDbType.VarChar;
pGen.Direction = ParameterDirection.Input;
pGen.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pAge.ParameterName = "#Age";
pAge.SqlDbType = SqlDbType.Int;
pAge.Direction = ParameterDirection.Input;
pAge.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pAdd1.ParameterName = "#Address1";
pAdd1.SqlDbType = SqlDbType.VarChar;
pAdd1.Direction = ParameterDirection.Input;
pAdd1.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pAdd2.ParameterName = "#Address2";
pAdd2.SqlDbType = SqlDbType.VarChar;
pAdd2.Direction = ParameterDirection.Input;
pAdd2.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pCity.ParameterName = "#City";
pCity.SqlDbType = SqlDbType.VarChar;
pCity.Direction = ParameterDirection.Input;
pCity.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pPhone.ParameterName = "#Phone";
pPhone.SqlDbType = SqlDbType.VarChar;
pPhone.Direction = ParameterDirection.Input;
pPhone.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pMobile.ParameterName = "#Mobile";
pMobile.SqlDbType = SqlDbType.VarChar;
pMobile.Direction = ParameterDirection.Input;
pMobile.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pEmail.ParameterName = "#Email";
pEmail.SqlDbType = SqlDbType.VarChar;
pEmail.Direction = ParameterDirection.Input;
pEmail.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}
There are a mass amounts of items here that are suspect.
Never do the auto incrementing yourself. All databases that I am aware of have the ability to do this for you.
Change your variable name for each parameter. Looks as though you got to pSurn and did some copying and pasting. This code just really needs clean up work more than anything.
This is really not much of an answer but I hope this moves you in the right direction.
try this
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pSurn.ParameterName = "#Gender";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pSurn.ParameterName = "#Age";
pSurn.SqlDbType = SqlDbType.Int;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pSurn.ParameterName = "#Address1";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pSurn.ParameterName = "#Address2";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pSurn.ParameterName = "#City";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pSurn.ParameterName = "#Phone";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pSurn.ParameterName = "#Mobile";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pSurn.ParameterName = "#Email";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}
in your age field you are using #Surname .change that and try again

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 };

Out parameters in stored procedures (oracle database)

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);

Categories