我尝试根据登录的 UserId 检索用户的数据,但没有任何进展。有什么建议么?
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
// Get a reference to the currently logged on user
MembershipUser currentUser = Membership.GetUser();
// Determine the currently logged on user's UserId value
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
// Assign the currently logged on user's UserId to the @UserId parameter
e.Command.Parameters["@UserId"].Value = currentUserId;
}
这是SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString='<%$ ConnectionStrings:NaviConnectionString %>'
SelectCommand="SELECT [UserId], [FirstName], [LastName] FROM [UserProfiles] WHERE ([UserId] = @UserId)">
<SelectParameters>
<asp:Parameter Name="UserId" Type="String"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>
这是GridView
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKFFeyNames="UserId" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
DataKeyNames="UserId">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" SortExpression="UserId" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
</Columns>
</asp:GridView>
没有要显示的数据记录。
但是,当我在查询生成器中执行代码时,它可以工作。
所以我需要UserId
从数据库中手动获取。
然后转到查询生成器,然后执行,然后将userid
in 粘贴到UserID
.
然后它根据以下内容显示正确的数据UserId
:
我也尝试将其更改为:
<SelectParameters>
<asp:Parameter Name="UserId" Type="Empty" DbType="Guid"></asp:Parameter>
</SelectParameters>