Reputation: 845
I am trying to call a stored procedure in a C# project that returns 2 values in addition to some input parameters in Oracle 11g. The procedure is a simple login that returns a varchar2 ('T' or 'F') (oracle does not accept booleans?) if there is a user stored that matches the username and password, and it should return a number (1 or 2) for the type of user that it is.
If I test the procedure with just the 'pexito'(success) output parameter it works, but it doesn't with the 'ptipo'(type of user)) parameter.
I tried changing the datatype of the 'ptipo' parameter to varchar2 and string in the c# code , parsing in different ways, converting to string and then to int and lots of other things. Nothing works, same error always.
"Unable to cast object of type 'System.String' to type 'Oracle.DataAccess.Client.OracleParameter'"
Here is the PL/SQL SP code:
CREATE OR REPLACE PROCEDURE sp_login
(puser IN VARCHAR2, ppass IN VARCHAR2, pexito OUT VARCHAR2, ptipo OUT NUMBER )
AS
lfila NUMBER;
ltipo number;
BEGIN
SELECT COUNT(*)
INTO lfila
FROM usuario
WHERE user_login = puser AND pass_login = ppass;
SELECT idtipo_user
INTO ltipo
FROM usuario
WHERE user_login = puser AND pass_login = ppass;
IF lfila = 0 THEN pexito:='F';
ELSE pexito:='T';
END IF;
IF ltipo =NULL THEN ptipo:=NULL;
ELSE ptipo:=ltipo;
END IF;
END;
/
And here is the C# code:
using System;
using System.Data;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
//using System.Data.OracleClient; DEPRECATED
using Sistema_On_Tour.Vistas;
using Sistema_On_Tour.Controlador;
private void BtnIniciar_Click(object sender, EventArgs e)
{
OracleConnection conn = new OracleConnection(Conexion.conn);
try
{
conn.Open();
OracleCommand cmd = new OracleCommand("sp_login", conn);
cmd.CommandType = CommandType.StoredProcedure;
OracleParameter paruser = new OracleParameter("puser", OracleDbType.Varchar2);
paruser.Value= TxtUser.Text;
paruser.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paruser);
OracleParameter parpass = new OracleParameter("ppass", OracleDbType.Varchar2);
parpass.Value = TxtPass.Text;
parpass.Direction = ParameterDirection.Input;
cmd.Parameters.Add(parpass);
OracleParameter parexito = new OracleParameter("pexito", OracleDbType.Varchar2);
parexito.Direction = ParameterDirection.Output;
parexito.Size = 1;
cmd.Parameters.Add(parexito);
OracleParameter ptipo = new OracleParameter("ptipo", OracleDbType.Int32);
ptipo.Direction = ParameterDirection.Output;
ptipo.Size = 1;
cmd.Parameters.Add("ptipo");
cmd.ExecuteNonQuery();
string exito = cmd.Parameters["pexito"].Value.ToString();
int tipouser = int.Parse(cmd.Parameters["ptipo"].Value.ToString());
if (exito.Equals('T'))
{
if (tipouser == 1)
{
this.Hide();
VentanaPrincipalApoderado v = new VentanaPrincipalApoderado();
v.Show();
}
else if(tipouser==2)
{
this.Hide();
VentanaPrincipalEjecutivo v = new VentanaPrincipalEjecutivo();
v.Show();
}
}
MessageBox.Show(exito);
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
finally
{
conn.Close();
}
}
}
}
Upvotes: 2
Views: 8204
Reputation: 21
This Kind of thing will not work
OracleParameter paruser = new OracleParameter("puser", OracleDbType.Varchar2);
paruser.Value= TxtUser.Text;
paruser.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paruser);
Do just like below code other wise you will get conversion error.
cmd.Parameters.Add("P_UserName", OracleDbType.Varchar2).Value = login.UserName;
cmd.Parameters.Add("P_UPassword", OracleDbType.Varchar2).Value = login.Password;
cmd.Parameters.Add("cur_splogin", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
Upvotes: 0
Reputation: 17485
There is a problem with Line
cmd.Parameters.Add("ptipo");
Instead you have to do
cmd.Parameters.Add(ptipo);
Note : You have to pass Variable not name of the variable as a string.
Upvotes: 1