La récupération de valeur unique à partir d'une requête

Je suis d'essayer de récupérer une valeur entière à partir d'un seul tableau, en fonction de la chaîne champ nom d'utilisateur. Je l'ai essayé à l'aide d'une procédure stockée, et directe de texte. Lorsque j'exécute la procédure stockée, - je obtenir la bonne valeur de retour; cependant, le résultat ne vient pas à travers.

Ici sont à la fois des jeux de code -
Texte Direct -

public int GetUserRole(string CUSER)
{
    try
    {
        SQLCON = new SqlConnection(connectionString);
        SQLCON.Open();
        SQLCommand = new SqlCommand();
        SQLCommand.CommandType = CommandType.Text;
        SQLCommand.Parameters.Add("USUsername", SqlDbType.VarChar).Value = CUSER;
        SQLCommand.CommandText = "SELECT USRole FROM tblUser WHERE USUsername = CUSER";
        Int32 USRole = (Int32) SQLCommand.ExecuteScalar();

        return USRole;

    }
    catch
    {
        HttpContext.Current.Response.Redirect("~/ErrorRedirect.aspx", false);
        return 0;
    }
}

Requête SQL:

ALTER PROCEDURE [dbo].[spGetUserRole]
-- Add the parameters for the stored procedure here
    @username VARCHAR(50)
AS
BEGIN
-- Declare the return variable here
DECLARE @USRole as int

-- Add the T-SQL statements to compute the return value here
SELECT @USRole = tblUser.USRole FROM tblUser WHERE USUsername = @username

-- Return the result of the function
RETURN @USRole  
END

OriginalL'auteur Cindy Brozyno | 2012-05-30