SqlDataSource - 带有会话变量过滤器的SelectCommand

时间:2016-07-08 21:40:07

标签: asp.net session sqldatasource selectcommand

ASP.NET 2.0框架

我正在尝试在用户访问该页面时列出所有与用户相关的条目。我在会话中设置了一个会话变量。现在我想用该ID查询数据库。它似乎正在执行,但没有返回任何结果(我在单独的代码部分显示内容)。

请帮忙!

<asp:SqlDataSource
ID="UserReports" 
runat="server"
ConnectionString= "<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand = "SELECT [ID], [ReportName], [ReportPath] FROM [Pan].          
[dbo].[Reports] WHERE ([VisitorID] = @VisitorID)"
OnSelecting  = "SqldsExample_Selecting">

<SelectParameters>
    <asp:Parameter Name="VisitorID" Type="String"/>
</SelectParameters>    
</asp:SqlDataSource>

On the code behind:

Sub SqldsExample_Selecting(sender As Object, e As      
SqlDataSourceSelectingEventArgs)

UserReports.SelectParameters.Add("@VisitorID", Session("VisitorID"))

End Sub

1 个答案:

答案 0 :(得分:0)

不要使用<asp:Parameter>。您应该使用<asp:SessionParameter>直接访问会话变量:

<SelectParameters>
    <asp:SessionParameter Name="VisitorID" SessionField="VisitorID" />
</SelectParameters> 
相关问题