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?
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.
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.