Click here to Skip to main content
15,908,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
May i know whats problem with my code, the procedure executed correct only, but here userId am getting zero (0)

C#
public static bool AdminLogin(string username, string password, out int userId)
    {
        bool IsUser = false;

        using (SqlConnection conn = new SqlConnection(Constants.CONNECTION))
        {
            using (SqlCommand cmd = new SqlCommand(Constants.Procs.CHECK_ADMIN_LOGINS, conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@Username", SqlDbType.VarChar, 20).Value = username;
                cmd.Parameters.Add("@Password", SqlDbType.VarChar, 20).Value = password;
                cmd.Parameters.Add("@UserId", SqlDbType.SmallInt).Value = ParameterDirection.Output;
                cmd.Parameters.Add("@IsUser", SqlDbType.Bit, 2).Value = ParameterDirection.Output;

                conn.Open();
                cmd.ExecuteNonQuery();

                int.TryParse(Convert.ToString(cmd.Parameters["@UserId"].Value), out userId);

                return Convert.ToBoolean(cmd.Parameters["@IsUser"].Value);
            }
        }
        return IsUser;
    }
Posted

1 solution

Why do you think that setting the Value of a parameter to "ParameterDirection.Output" is going to magically cause it to become an output parameter? Do you think that setting it to your username magically makes it your user?

Try this:
C#
cmd.Parameters.Add("@UserId", SqlDbType.SmallInt).Direction = ParameterDirection.Output;
 
Share this answer
 
Comments
NagarajDJ 26-Oct-13 7:11am    
i declared @UserId as output parameter in procedure so, thats why am send it as output parameter
OriginalGriff 26-Oct-13 7:21am    
Yes, I guessed that. But you don't tell the SqlCommand it's an output parameter by setting teh Value - you do it by setting the Direction...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900