Anthony Knight
Anthony Knight

Reputation: 63

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.

// 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. However when the where clause is in place, I get the error: "Procedure or Function 'getClients' expects parameter '@UserId', which was not supplied." Could I get some help please?

Upvotes: 1

Views: 2603

Answers (1)

Jacob
Jacob

Reputation: 3698

You should supply the UserId to your Stored Procedure from your C# code like this:

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.

Upvotes: 1

Related Questions