Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameter to stored procedure in C#

I have a stored procedure that returns a variable @result set to 1 or 0 (datatype bit). I am accessing it in my C# with the following code. Its throwing an error saying too many parameters.

protected void btnRegister_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
    con.Open();

    SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);
    Cmd.CommandType = CommandType.StoredProcedure;
    Cmd.CommandText = "Registration";
    Cmd.Parameters.AddWithValue("@Name", txtName.Text);
    Cmd.Parameters.AddWithValue("@Email", txtEmailAddress.Text);
    Cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
    Cmd.Parameters.AddWithValue("@CountryCode", ddlCountryCode.Text);
    Cmd.Parameters.AddWithValue("@Mobile", txtMobileNumber.Text);
    //Cmd.Parameters.Add("@Result", DbType.Boolean);
    SqlParameter sqlParam = new SqlParameter("@Result", DbType.Boolean);
    //sqlParam.ParameterName = "@Result";
    //sqlParam.DbType = DbType.Boolean;
    sqlParam.Direction = ParameterDirection.Output;
    Cmd.Parameters.Add(sqlParam);
    Cmd.ExecuteNonQuery();
    con.Close();
    Response.Write(Cmd.Parameters["@Result"].Value); 
}

the stored procedure: (this I think is fine...) And please correct my CS code...

ALTER PROCEDURE [dbo].[usp_CheckEmailMobile](
    @Name VARCHAR(50), 
    @Email NVARCHAR(50), 
    @Password NVARCHAR(50), 
    @CountryCode INT, 
    @Mobile VARCHAR(50), 
    @Result BIT OUTPUT)
    AS 
BEGIN 

IF EXISTS (SELECT COUNT (*) FROM AUser WHERE  [Email] = @Email AND [Mobile] = @Mobile) 
Begin 
    Set @Result=0; --Email &/or Mobile does not exist in database
End
ELSE
Begin
    --Insert the record & register the user 
    INSERT INTO [AUser] ([Name], [Email], [Password], [CountryCode], [Mobile]) VALUES (@Name, @Email, @Password, @CountryCode, @Mobile)  
    Set @Result=1;
End
END
like image 395
who-aditya-nawandar Avatar asked Mar 22 '13 12:03

who-aditya-nawandar


People also ask

Can we pass parameters to stored procedures?

You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.

How do you pass a parameter to a procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

How do you pass a parameter to a stored procedure while executing?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.


2 Answers

you can try this code :

bool result=false;
SqlCommand scCommand = new SqlCommand("usp_CheckEmailMobile", sqlCon);
scCommand.CommandType = CommandType.StoredProcedure;
scCommand.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text;
scCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = txtEmailAddress.Text;
scCommand.Parameters.Add("@Password ", SqlDbType.NVarChar, 50).Value = txtPassword.Text;
scCommand.Parameters.Add("@CountryCode", SqlDbType.VarChar.50).Value =ddlCountryCode.SelectedText;
scCommand.Parameters.Add("@Mobile", SqlDbType.NVarChar, 50).Value = txtMobileNumber.Text;
scCommand.Parameters.Add("@Result ", SqlDbType.Bit).Direction = ParameterDirection.Output;
try
{
    if (scCommand.Connection.State == ConnectionState.Closed)
    {
        scCommand.Connection.Open();
    }
    scCommand.ExecuteNonQuery();
    result = Convert.ToBoolean(scCommand.Parameters["@Result"].Value);


}
catch (Exception)
{

}
finally
{                
    scCommand.Connection.Close();
    Response.Write(result); 
}
like image 159
Arshad Avatar answered Sep 21 '22 09:09

Arshad


Why do you set:

Cmd.CommandText = "Registration";

this will replace your stored procedure name, so it won't call the stored procedure you indicated in:

SqlCommand Cmd = new SqlCommand("usp_CheckEmailMobile", con);

It can be useful to use a SQL profiler to debug that the SQL going "over the wire" is as expected.

like image 24
Rowland Shaw Avatar answered Sep 21 '22 09:09

Rowland Shaw