简体   繁体   中英

Gettitng error as Procedure or function expects parameter which was not supplied

I have created a Stored procedure to Insert my data into the table, but I am getting error as

Procedure or function 'AddUserDetails' expects parameter '@UserId', which was not supplied.

Here is my SP

CREATE PROCEDURE AddUserDetails 
@UserId nvarchar(55),
@UserPassword nvarchar(100),
@ConfirmPass nvarchar(100),
@Mobile int,
@Email nvarchar(100),
@BirthDate nvarchar(100)
AS  
BEGIN
    SET NOCOUNT ON;


     Insert into RegisterUser(UserId,UserPassword,ConfirmPass, Mobile, Email,BirthDate)Values (@UserId, @UserPassword, @ConfirmPass, @Mobile, @Email,@BirthDate)
END
GO

Also here is my C# code.

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add("@UserId", SqlDbType.NVarChar).Value = txtUserId.Text;
            cmd.Parameters.Add("@UserPassword", SqlDbType.NVarChar).Value = txtPassword.Text;
            cmd.Parameters.Add("@ConfirmPassword", SqlDbType.NVarChar).Value = txtConfirmPassword.Text;
            cmd.Parameters.Add("@Mobile", SqlDbType.Int).Value = txtMobile.Text;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = txtEmail.Text;
            cmd.Parameters.Add("@BirthDate", SqlDbType.NVarChar).Value = txtBirth.Text;
            cmd = new SqlCommand("AddUserDetails", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Redirect("http://www.google.com");
            con.Close();
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

Kindly suggest what is the mistake here

Because you are re-create your command with

cmd = new SqlCommand("AddUserDetails", con);

line and you never add any parameter to that cmd. You try to add old one with created SqlCommand cmd = new SqlCommand(); line.

Delete this SqlCommand cmd = new SqlCommand(); line and move your;

SqlCommand cmd = new SqlCommand("AddUserDetails", con);
cmd.CommandType = CommandType.StoredProcedure;

top of your code. That's it. And you never done anything in your catch part. Just throwed new exception with throw ex; but this resets the stack trace . And consider to use using statement to dispose your connections and commands automatically instead of calling Close() or Dispose() methods manually.

    try
    {
        SqlCommand cmd = new SqlCommand("AddUserDetails", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@UserId", SqlDbType.NVarChar).Value = txtUserId.Text;
        cmd.Parameters.Add("@UserPassword", SqlDbType.NVarChar).Value = txtPassword.Text;
        cmd.Parameters.Add("@ConfirmPassword", SqlDbType.NVarChar).Value = txtConfirmPassword.Text;
        cmd.Parameters.Add("@Mobile", SqlDbType.Int).Value = Convert.ToInt32(txtMobile.Text);
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = txtEmail.Text;
        cmd.Parameters.Add("@BirthDate", SqlDbType.NVarChar).Value = txtBirth.Text;
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Redirect("http://www.google.com");
        con.Close();
    }
    catch (Exception ex)
    {
        // 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM