Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8767223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:45:21+00:00 2026-06-13T16:45:21+00:00

I have repeatedly test this #blankety-blank# stored procedure on SQL Server, and it returns

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:45:22+00:00Added an answer on June 13, 2026 at 4:45 pm

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

    And use SELECT instead of return in your sql code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like: $('.test').hover( function(){ $(this).animate(...); }, function(){ $(this).animate(...); } ); But if
I have been repeatedly asked the following questions in many interviews.... But still can't
for (;;) { //Something to be done repeatedly } I have seen this sort
In my code I have a server process repeatedly probing for incoming messages, which
(This is sort of a long-winded question but I have summarized it at the
At our current project we have 1. a production server, 2. a test server
Whether this is possible I don't know, but it would mighty useful! I have
I have data which looks like this (this is test data for illustration): test
I have been dancing around this issue for awhile but it keeps coming up.
I know, this question already been asked in many places repeatedly. But I've tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.