An SqlParameter with ParameterName '@UserId' is not contained by this SqlParameterCollection

后端 未结 4 1677
清酒与你
清酒与你 2021-01-15 14:36

I had a login page. once user successfuly logged in, they can view and manage their profile/information. This would be done by retrieving data from database and display on a

相关标签:
4条回答
  • 2021-01-15 15:24

    If you already have the parameter listed in your SqlDataSource, then simply point to it and change it's value ...

    e.Command.Parameters["@UserId"].Value = currentUserId;
    

    You do not need to create a parameter, unless you have not listed it back in your ASP page.

    0 讨论(0)
  • 2021-01-15 15:25

    Create a new SqlParameter and add it to the collection.

    SqlParameter param = new SqlParameter("@UserId", currentUserId);
    e.Command.Parameters.Add(param);
    
    0 讨论(0)
  • 2021-01-15 15:26

    Try the following:

    DbParameter param = e.Command.CreateParameter();
    param.ParameterName = "@UserId";
    param.Value = currentUserId;
    e.Command.Parameters.Add(param);
    

    I didn't test this though

    0 讨论(0)
  • 2021-01-15 15:29

    You must add the parameter with

    e.Command.Parameters.AddWithValue("@UserId", currentUserId);
    

    Once you have added it you could access it through the indexer as in your example.


    UPDATE

    If you are working with the System.Data.Common namespace, the AddWithValue method is not available. You will have to do something like

    var param = e.Command.CreateParameter("@UserId", currentUserId);
    e.Command.Parameters.Add(param);
    

    This is a little bit more complicated but has the advantage that you do not have to implicitly create a parameter of a specific type like SqlParamter or OleDbParameter.

    0 讨论(0)
提交回复
热议问题