How to pass a variable to the SelectCommand of a SqlDataSource?

后端 未结 10 657
耶瑟儿~
耶瑟儿~ 2020-12-01 00:37

I want to pass variable from the code behind to the SelectCommand of a SqlDataSource?

I don\'t want to use built-in parameter types (like ControlParameter, QueryStri

10条回答
  •  借酒劲吻你
    2020-12-01 00:48

    Try this instead, remove the SelectCommand property and SelectParameters:

    
    

    Then in the code behind do this:

    SqlDataSource1.SelectParameters.Add("userId", userId.ToString());
    
    SqlDataSource1.SelectCommand = "SELECT items.name, items.id FROM items INNER JOIN users_items ON items.id = users_items.id WHERE (users_items.user_id = @userId) ORDER BY users_items.date DESC"
    

    While this worked for me, the following code also works:

    
    
    
    SqlDataSource1.SelectParameters.Add("userid", DbType.Guid, userId.ToString());
    

提交回复
热议问题