简体   繁体   English

过程或函数预期未提供参数错误

[英]Procedure or Function expects parameter error which was not supplied

I'm using the database created via ASP.NET Configuration Manager, After logging in, I have a page that show the user's client in a Gridview, but I only want the user to see their respective clients only. 我使用的是通过ASP.NET Configuration Manager创建的数据库,登录后,我有一个页面在Gridview中显示用户的客户端,但是我只希望用户仅看到各自的客户端。

// the code: // 编码:

protected void Page_Load(object sender, EventArgs e)
    {


        String strConnString = ConfigurationManager.ConnectionStrings["BA_2014ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "getClients";
        cmd.Connection = con;

            con.Open();
            gvClients.DataSource = cmd.ExecuteReader();
            gvClients.DataBind();

            con.Close();
            con.Dispose();

    }

// the stored procedure: //存储过程:

ALTER PROCEDURE dbo.getClients
@UserId uniqueidentifier
AS
BEGIN
    SELECT ClientId, UserId, ClientName, EmailAddress, PhoneNumber, ServicesNum FROM  Client_tb WHERE @UserId = UserId  

END

...when I remove the where clause, the gridview shows all the clients. ...当我删除where子句时,gridview显示所有客户端。 However when the where clause is in place, I get the error: "Procedure or Function 'getClients' expects parameter '@UserId', which was not supplied." 但是,当where子句到位时,出现错误:“过程或函数'getClients'期望未提供参数'@UserId'。” Could I get some help please? 我可以帮忙吗?

You should supply the UserId to your Stored Procedure from your C# code like this: 您应该像这样从C#代码向存储过程提供UserId:

cmd.Parameters.Add("@UserId", SqlDbType.bytes).Value = txtUserId.Text;

Im not sure about the SqlDbType.bytes because i'm not near Visual Studio at the moment but it should be something like that. 我不确定SqlDbType.bytes因为我目前不在Visual Studio附近,但应该是类似的东西。

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

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