I have created a PL/SQL function and stored it in my oracle database.Now I want to call that function on clicking a button.
I am using visual studio and c#.
My humble attempt-
private void button3_Click(object sender, EventArgs e)
{
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
String x1 = textBox1.Text;
String x2 = textBox2.Text;
comm.CommandText = "log_in";
comm.Parameters.Add(new OracleParameter("c", OracleDbType.Varchar2, textBox1.Text, ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter("tt", OracleDbType.Varchar2, 256, ParameterDirection.ReturnValue));
String z = comm.Parameters["z"].Value.ToString();
comm.ExecuteNonQuery();
if (z.Equals('1'))
{ MessageBox.Show("correct"); }
conn.Close();
}
PL/SQL-
CREATE OR REPLACE FUNCTION log_in
(
x IN VARCHAR2,
y IN VARCHAR2
) RETURN VARCHAR2 AS
match_count NUMBER;
BEGIN
SELECT COUNT(*)
INTO match_count
FROM student_login
WHERE email = x
AND password = y;
IF match_count = 0 THEN
RETURN 0;
ELSE
RETURN 1;
END IF;
END;
note-my data source is .NET framework data provider for oracle and the answer is for odp.net
Your function has two input parameters, thus your call must also define two input parameter plus the return parameter. Method Add(OracleParameter) is possible but redundant.
It should be like this:
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
comm.CommandText = "log_in";
comm.Parameters.Add("x", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox1.Text;
// As far as I remember "ParameterDirection.Input" is the default, so you may skip it
comm.Parameters.Add("y", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox2.Text;
comm.Parameters.Add("ret", OracleDbType.Byte, ParameterDirection.ReturnValue);
comm.Parameters["ret"].DbType = DbType.Byte;
comm.ExecuteNonQuery();
String returnValue = comm.Parameters["ret"].Value.ToString();
Actually I never used CommandType.StoredProcedure. If the code above does not work try this one instead:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := log_in(:x, :y); END;";
Hi I had some problems with StoredProcedure. And finally get it working.
comm = new OracleCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
comm.CommandText = "log_in";
// Important is to add returnValue as described bellow
OracleParameter retval = new OracleParameter("ret", OracleDbType.Varchar2 , 20);
retval.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(retval);
comm.Parameters.Add("x", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox1.Text;
comm.Parameters.Add("y", OracleDbType.Varchar2, ParameterDirection.Input).Value = textBox2.Text;
comm.ExecuteNonQuery();
String returnValue = comm.Parameters["ret"].Value.ToString();
Related
Hi I am trying to call an as400 stored procedure using OleDB. Could you please post an example of how to do it cause I've been following some tutorials but not matter what I do I always get an Invalid Token Exception
this is what I do
OleDbCommand sp = new OleDbCommand("CALL NASVARWG.SP001(?,?,?,?,?) ", connectionDB);
sp.CommandType = CommandType.StoredProcedure;
sp.Parameters.Add("P1", System.Data.OleDb.OleDbType.Char).Value = "ESANASTRIS";
sp.Parameters["P1"].Size = 10;
sp.Parameters["P1"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P2", System.Data.OleDb.OleDbType.Char).Value = "SAMNAS";
sp.Parameters["P2"].Size = 10;
sp.Parameters["P2"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P3", System.Data.OleDb.OleDbType.Char).Value = textBox_Reparto.Text;
sp.Parameters["P3"].Size = 6;
sp.Parameters["P3"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P4", System.Data.OleDb.OleDbType.Char).Value = "we can do this";
sp.Parameters["P4"].Size = 60;
sp.Parameters["P4"].Direction = ParameterDirection.Input;
sp.Parameters.Add("P5", System.Data.OleDb.OleDbType.Char).Value = "help";
sp.Parameters["P5"].Size = 256;
sp.Parameters["P5"].Direction = ParameterDirection.Input;
sp.Prepare();
sp.ExecuteNonQuery();
the exception I get says "NASVARWG" is not a valid token. Why? that is the name of the library containing the procedure and the spelling is correct.
Thanks for your help
A C# code with CommandType.StoredProcedure example :
// assume a DB2Connection conn
DB2Transaction trans = conn.BeginTransaction();
DB2Command cmd = conn.CreateCommand();
String procName = "INOUT_PARAM";
cmd.Transaction = trans;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
A C# code with CommandType.Text example :
// assume a DB2Connection conn
DB2Transaction trans = conn.BeginTransaction();
DB2Command cmd = conn.CreateCommand();
String procName = "INOUT_PARAM";
String procCall = "CALL INOUT_PARAM (#param1, #param2, #param3)";
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;
cmd.CommandText = procCall;
// Register input-output and output parameters for the DB2Command
cmd.Parameters.Add( new DB2Parameter("#param1", "Value1");
cmd.Parameters.Add( new DB2Parameter("#param2", "Value2");
DB2Parameter param3 = new DB2Parameter("#param3", IfxType.Integer);
param3.Direction = ParameterDirection.Output;
cmd.Parameters.Add( param3 );
// Call the stored procedure
Console.WriteLine(" Call stored procedure named " + procName);
cmd.ExecuteNonQuery();
// Register input-output and output parameters for the DB2Command
...
// Call the stored procedure
Console.WriteLine(" Call stored procedure named " + procName);
cmd.ExecuteNonQuery();
I am trying to execute a stored procedure in c#.
Stored procedure performs insert operation and returns values of one of the columns as output parameter.
I need to execute it from c# and get back value of output param.
Below is what i have tried so far.
SqlParameter[] associateParams = new SqlParameter[10];
{
associateParams[0]=new SqlParameter("#orgName", newAssociate.OrgName);
associateParams[1]=new SqlParameter("#createdBy", newAssociate.Email);
associateParams[2]=new SqlParameter("#userName", newAssociate.UserName);
associateParams[3]=new SqlParameter("#workEmail", newAssociate.Email);
associateParams[4]=new SqlParameter("#password", newAssociate.Password);
associateParams[5]=new SqlParameter("#teamStrength", "0");
associateParams[6]=new SqlParameter("#projName", newAssociate.ProjName);
associateParams[7]=new SqlParameter("#userType", "Associate");
associateParams[8] = new SqlParameter("#userSalt", SqlDbType.VarChar, 400);
associateParams[8].Direction = ParameterDirection.Output;
associateParams[9] = new SqlParameter("#activationKey", SqlDbType.Int);
associateParams[9].Direction = ParameterDirection.Output;
}
using (SqlCommand cmd = con.CreateCommand())
{
log.Debug("In command is called");
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = ProcedureName;
cmd.Parameters.AddRange(param);
log.Debug("Command is called");
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
log.Debug("Con is open");
}
cmd.ExecuteScalar();
log.Debug(cmd.Parameters["#userSalt"].Value.ToString());
log.Debug(cmd.Parameters["#activationKey"].Value.ToString());
Executing above, performs insert successfully but returns null for output params values.
Can anyone suggest what I am missing here.
Thanks
try like this when you define output parameters:
associateParams[8] = new SqlParameter("#userSalt", SqlDbType.VarChar, 400);
associateParams[8].Value = "";
associateParams[8].Direction = ParameterDirection.Output;
associateParams[9] = new SqlParameter("#activationKey", SqlDbType.Int);
associateParams[9].Value = 0;
associateParams[9].Direction = ParameterDirection.Output;
let me know if this helps.
UPDATE: here is my own method
cmd.Parameters.Add(new SqlParameter("#userSalt", SqlDbType.VarChar, 400));
cmd.Parameters["#userSalt"].Value = "";
cmd.Parameters["#userSalt"].Direction = ParameterDirection.Output;
UPDATE1: because you dont use ExecuteNonQuery. Change cmd.ExecuteScalar() to cmd.ExecuteNonQuery()
I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}
You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);
First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value
I'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 :-(
I have a register form and I want to check that the user did not register before.
My code is in below. I think two problems exist: (1) the call method and (2) passing the parameter to the stored procedure. My symptom is that this causes an exception that says the input parameter not initialized.
create procedure fakeuser #username nvarchar(250),#codemeli nchar(10),#email nvarchar(50), #user nvarchar(250) output,#code nchar(10)output,#mail nvarchar(50)output
as
if exists(select username,email,codemeli from karbar where username=#username)
set #user=#username
else if exists(select username,email,codemeli from karbar where codemeli=#codemeli)
set #code=#codemeli
else if exists(select username,email,codemeli from karbar where email=#email)
set #mail= #email
Here is the c# code:
public static string confirm(string username, string email, string codemeli)
{
string constring = "data source=.;database=site;integrated security=true;";
SqlConnection connection = new SqlConnection(constring);
// Command - specify as StoredProcedure
SqlCommand command = new SqlCommand("fakeuser", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("#username", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Input;
param.Value = username;
command.Parameters.Add(param);
SqlParameter param2 = new SqlParameter("#email", SqlDbType.NVarChar);
param2.Direction = ParameterDirection.Input;
param2.Value = username;
command.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("#codemeli", SqlDbType.NChar);
param3.Direction = ParameterDirection.Input;
param3.Value = username;
command.Parameters.Add(param3);
// Return value as parameter
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar);
returnuser.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnuser);
SqlParameter returncode = new SqlParameter("#code", SqlDbType.NChar);
returncode.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returncode);
SqlParameter returnmail = new SqlParameter("#mail", SqlDbType.NVarChar);
returnmail.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnmail);
// Execute the stored procedure
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Thank you.
Set ParameterDirection=Output for #user,#code, and #mail and also specify the width/size/length of parameter.
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar,100);
returnuser.Direction = ParameterDirection.Output;
command.Parameters.Add(returnuser);