繁体   English   中英

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

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

我已经创建了一个存储过程以将数据插入表中,但是由于

过程或函数“ AddUserDetails”需要未提供的参数“ @UserId”。

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

行,并且您永远不会向该cmd添加任何参数。 您尝试使用创建的SqlCommand cmd = new SqlCommand();添加旧版本SqlCommand cmd = new SqlCommand(); 线。

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

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

代码的顶部。 而已。 而且,您在捕获方面从未做过任何事情。 刚刚用throw ex;新异常throw ex; 但这会重置堆栈跟踪 并考虑使用using语句自动处理连接和命令,而不是手动调用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)
    {
        // 
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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