user6789854
user6789854

Reputation: 23

Call SQL Server stored procedure with parameters using C#

I am trying to call a very simple SQL Server stored procedure using C# code. I have a class which has authenticate method. I am passing text box values (userID, password) to this method and it keeps on throwing me error about required parameter not being passed. I am basically a Business Intelligence professional working on C# project. Help will be appreciated.

Here is the code I am executing:

sqcon.Open();

SqlCommand cmd = new SqlCommand("Users.PR_Authenticate_WebUsers",sqcon);

cmd.Parameters.Add("@In_UserID", SqlDbType.VarChar).Value = "f";
cmd.Parameters.Add("@In_PassWord", SqlDbType.NVarChar).Value = "f";
cmd.Parameters.Add("@Out_IsAuthenticatedUser", SqlDbType.Bit);
cmd.Parameters["@Out_IsAuthenticatedUser"].Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();
sqcon.Close();

I don't understand when I am passing parameter values explicitly why it complains about value not being passed? Am I missing something?

Upvotes: 2

Views: 9351

Answers (1)

Zain Ul Abidin
Zain Ul Abidin

Reputation: 2710

Hmmm looks like you are not telling the command object that it is a stored procedure not a regular query try this one

    sqcon.Open();
    SqlCommand cmd = new SqlCommand("Users.PR_Authenticate_WebUsers",sqcon);
    cmd.CommandType = CommandType.StoredProcedure;

    //Add parameters like this
    cmd.Parameters.Add(new SqlParameter("@In_UserID", "f"));
    sqcon.Close()

Upvotes: 4

Related Questions