How to properly remove the return value from a call to a stored procedure

First of all, I would like to thank anyone who reads this and responds. Your help is greatly appreciated!

I am developing an ASP.NET web application in which I need to read and write to a database. For this, I was trying to call from my C # code several stored procedures that I wrote. In particular, these are:

public static bool userExistsInDB(string username)
{
    int userExistsInDB = -1;
    using (SqlConnection con = new SqlConnection(DBConfig.DbConnectString))
    {
        using (SqlCommand cmd = new SqlCommand("tb_user_exists", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            con.Open();
            userExistsInDB = cmd.ExecuteNonQuery();
        }
    }
    return userExistsInDB == 1;
}

      

The stored procedure I called was created using this SQL script:

CREATE PROCEDURE tb_is_logged_in
    @username VARCHAR(20)
AS
IF(EXISTS(SELECT * FROM logged_in_users WHERE "username" = @username))
BEGIN
    RETURN 1; 
END
ELSE
BEGIN
    RETURN 0; 
END

      

I tested it myself in SQL Server Management Studio and it seems to work. However, after navigating to my C # code, I found an issue with the line

userExistsInDB = cmd.ExecuteNonQuery();

      

The stored procedure should return 1 or 0 if the specified user exists in the database or not, respectively, and, as you can see, is userExistsInDB

initialized with -1

. However, after execution, the line userExistsInDB = cmd.ExecuteNonQuery();

userExistsInDB

never changes; it always retains its value -1

. So it seems that I am returning the value incorrectly. What am I doing wrong?

EDIT:

Thanks to Leonel, I found a solution to the problem. First, I realized that the stored procedure I inserted above was the wrong procedure. This is not the one I actually call in my C # code, and when I tried to fix Leonel it didn't work because I accidentally edited the procedure I inserted above instead of the one I'm actually calling. Here is the actual stored procedure I was calling:

CREATE PROCEDURE tb_user_exists
    @username VARCHAR(20)
AS
IF(EXISTS (SELECT * FROM users WHERE username = @username))
BEGIN
    RETURN 1;  
END
ELSE
BEGIN
    RETURN 0; 
END

      

My solution is this: change this line

userExistsInDB = cmd.ExecuteNonQuery();

      

to

userExistsInDB = (int)cmd.ExecuteScalar();

      

and change the stored procedure to:

CREATE PROCEDURE tb_user_exists
    @username VARCHAR(20)
AS
IF(EXISTS (SELECT * FROM users WHERE username = @username))
BEGIN
    SELECT 1;  
END
ELSE
BEGIN
    SELECT 0; 
END

      

+3


source to share


2 answers


Use method SqlCommand.ExecuteScalar();

from MSDN

For example:

userExistsInDB = (int)cmd.ExecuteScalar();

      

The example code will execute SqlCommand

and get the first row of the table, if exists, and set the value of the variable to the number of elements in the table. In this case, you can remove the return statement and use



SELECT * FROM logged_in_users WHERE "username" = @username

      

And also always close the connection after using it

con.Open();
userExistsInDB = cmd.ExecuteScalar();
con.Close();

      

+1


source


If you only get one value, executeScalar () will be good enough. If you still have problems, you can ask



String get;
Get=cmd.executescalar().tostring();

      

-1


source







All Articles