Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# stored procedure returning -1 Repeatedly

I have repeatedly test this #blankety-blank# stored procedure on SQL Server, and it returns either 0 or number of rows, but in C# I am always getting -1. I have been debugging this #blank# thing for hours and always -1.

Here's the stored procedure code.

Create procedure [dbo].[sp_enter_new_student] 
    @Prenom_detudiant [varchar](50)  = NULL, 
    @nom_detudiant [varchar](50)  = NULL,
    @nationalite_detudiant [varchar](40)  = NULL,
    @langue_parle [varchar](15)  = NULL,
    @nombre_denfants [int] = NULL,
    @sexe_detudiant [char](1) = NULL,
    @program_pk [int] = NULL,
    @Numero_detudiant int = NULL
As 
Declare @numOfRowsBefore int = 0; 
declare @numOfRowsAfter int = 0; 
declare @Error int = -100; 
BEGIN  
    SET NOCOUNT ON; 
    select @numOfRowsBefore = COUNT(*) from tbl_students where Numero_detudiant = @Numero_detudiant; 

    if (@numOfRowsBefore > 0)
    begin
        Print "Student already exists, Insertion will fail!";
        Print "Insertion Failure ! " + CONVERT(varchar(10), @numOfRowsBefore);
        return @numOfRowsBefore; -->  -1 indicates insertion failure
    End
    else if (@numOfRowsBefore = 0)
    begin
        Print "Student doesn't exists, Insertion will be Success!";
        Print "Insertion Success ! " + CONVERT(varchar(10), @numOfRowsBefore);
        return @numOfRowsBefore; -->  -1 indicates insertion failure
    End

END 

And here's a C# code


public int enregistreNouveauEtudiant(string Prenom_detudiant, string nom_detudiant, string nationalite_detudiant, string langue_parle, string nombre_denfants, string sexe_detudiant, string program_pk, string Numero_detudiant)
{
    int numberOfRowsInserted = 0;
    //try
    //{ 

    SqlConnection connection = this.GetConnection();
    SqlCommand insertStudentCommand = new SqlCommand("sp_enter_new_student", connection);
    connection.Open();
    insertStudentCommand.CommandType = CommandType.StoredProcedure;

    //insertStudentCommand.Parameters.Add("@ID", SqlDbType.Int);
    //insertStudentCommand.Parameters["@ID"].Direction = ParameterDirection.Output;

    insertStudentCommand.Parameters.Add("@Prenom_detudiant", SqlDbType.VarChar, 50).Value = Prenom_detudiant;
    insertStudentCommand.Parameters.Add("@nom_detudiant", SqlDbType.VarChar, 50).Value = nom_detudiant;
    insertStudentCommand.Parameters.Add("@nationalite_detudiant", SqlDbType.VarChar, 40).Value = nationalite_detudiant;
    insertStudentCommand.Parameters.Add("@langue_parle", SqlDbType.VarChar, 15).Value = langue_parle;
    insertStudentCommand.Parameters.Add("@nombre_denfants", SqlDbType.Int).Value = nombre_denfants;
    insertStudentCommand.Parameters.Add("@sexe_detudiant", SqlDbType.Char, 1).Value = sexe_detudiant;
    insertStudentCommand.Parameters.Add("@program_pk", SqlDbType.Int).Value = program_pk;
    insertStudentCommand.Parameters.Add("@Numero_detudiant", SqlDbType.Int).Value = Numero_detudiant;

    numberOfRowsInserted = insertStudentCommand.ExecuteNonQuery();

    connection.Close();

    return numberOfRowsInserted;
}

Can anyone have a look at this problem please? I used try and catch block as well, it's absolutely useless in this case.

Thanks.

like image 237
Junior CSharp Avatar asked Nov 01 '12 19:11

Junior CSharp


People also ask

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

You should use ExecuteScalar instead of ExecuteNonQuery in your c# code

And use SELECT instead of return in your sql code

like image 91
GregM Avatar answered Sep 20 '22 02:09

GregM