Gettitng 错误,因为过程或函数需要未提供的参数

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

我创建了一个存储过程来将我的数据插入 table, 但我收到错误消息

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

这是我的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

这也是我的 C# 代码。

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;
        }
    }

请指出这里的错误是什么

因为您正在使用

重新创建命令
cmd = new SqlCommand("AddUserDetails", con);

行,你从不向该命令添加任何参数。您尝试使用已创建的 SqlCommand cmd = new SqlCommand(); 行添加旧的。

删除此 SqlCommand cmd = new SqlCommand(); 行并移动您的;

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

代码的顶部。而已。而且您从未在捕获部分做过任何事情。刚刚使用 throw ex; 抛出新异常,但 this resets the stack trace. And consider to use using statement 自动处理您的连接和命令,而不是手动调用 Close()Dispose() 方法。

    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)
    {
        // 
    }