Para eso, debes de utilizar parámetros de salida
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("dbo.AgregarInscripcion");
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter= new SqlParameter();
parameter.Direction = ParameterDirection.Input;
parameter.SqlDbType = SqlDbType.Int;
parameter.ParameterName = "@Ci";
parameter.Value = Ci;
cmd.Parameters.Add(parameter);
SqlParameter parameter2 = new SqlParameter();
parameter2.Direction = ParameterDirection.Output;
parameter2.ParameterName = "@NumInsert";
parameter2.SqlDbType = SqlDbType.Int;
parameter2.Value = 0;
cmd.Parameters.Add(parameter2);
SqlParameter parameter3 = new SqlParameter();
parameter3.Direction = ParameterDirection.Input;
parameter3.SqlDbType = SqlDbType.VarChar;
parameter3.Size = 15;
parameter3.ParameterName = "@IDECURSO";
parameter3.Value = IdeCurso;
cmd.Parameters.Add(parameter3);
SqlParameter parameter4 = new SqlParameter();
parameter4.Direction = ParameterDirection.Input;
parameter4.SqlDbType = SqlDbType.VarChar;
parameter4.Size = 15;
parameter4.ParameterName = "@Empleado";
parameter4.Value = Empleado;
cmd.Parameters.Add(parameter4);
cmd.ExecuteNonQuery();
result = Convert.ToInt32(cmd.Parameters["@NumInsert"].Value);
}
En el procedure, tienes que retocar varias cosas.
A nivel de SQL.
CREATE OR ALTER PROCEDURE dbo.AGREGARINSCRIPCION
(
@CI int
, @IDECURSO varchar(15)
, @EMPLEADO varchar(15)
, @NUMINSERT int OUTPUT
)
AS
begin
if not exists (SELECT CEDULA FROM ALUMNO WHERE CEDULA = @CI )
Set @NUMINSERT = -1;
ELSE
BEGIN
if not exists (SELECT IDECURSO FROM CURSO WHERE IDECURSO = @IDECURSO )
Set @NUMINSERT = -2;
ELSE
if exists (SELECT IDECURSO FROM INSCRIPCION WHERE IDECURSO = @IDECURSO AND CEDULA = @CI)
Set @NUMINSERT = -3;
ELSE
BEGIN
INSERT INTO INSCRIPCION(CEDULA
, IDECURSO
, FECHAINSCRIPCION
, EMPLEADO)
VALUES
( @CI, @IDECURSO, GETDATE(), @EMPLEADO );
Set @NUMINSERT = SCOPE_IDENTITY();
END;
END;
RETURN;
END;
GO