I've got a really simple stored procedure that returns to me some output variables.
If I execute this stored procedure by any method (Management Studio, Visual Studio Server Explorer), it works pretty fine.
The thing is, if I try to execute it with ExecuteNonQuery()
on my client side, it doesn't execute the stored procedure, the ExecuteNonQuery()
returns the value -1 and there are no outputs. I want to know if I should be using something other than ExecuteNonQuery()
to execute this procedure and get to get these output variables.
This is my stored procedure:
CREATE PROCEDURE [dbo].ChekEmail
@Email varchar(50),
@Id int output,
@Flag_User int output
AS
SET @Id = 0
SET @Id = (SELECT Client.Id FROM Client WHERE Client.Email = @Email)
IF(@Id > 0)
BEGIN
SET @Flag_User = 1
RETURN @Id
END
ELSE
BEGIN
SET @Id = (SELECT Clinic.Id FROM Clinic WHERE Clinic.Email = @Email)
IF(@Id > 0)
BEGIN
SET @Flag_User = 2
RETURN @Id
END
END
and this is where I execute it
public int CheckEmail(string Email)
{
Conection con = new Conection();
SqlCommand cmd = new SqlCommand("ChekEmail");
cmd.Connection = con.connect();
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.Add("@Id", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@Flag_User", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.CommandType = CommandType.StoredProcedure;
int x = cmd.ExecuteNonQuery();
if (x >= 0)
{
int Id_Owner = Convert.ToInt32(cmd.Parameters["@Id"].Value.ToString());
int Flag_User = Convert.ToInt32(cmd.Parameters["@Flag_User"].Value.ToString());
if (Flag_User == 1)
{
return Id_Owner;
}
else
{
return 0;
}
}
else
{
return 0;
}
}