简体   繁体   中英

get sql data based on logged in user

i am using Visual Studio 2010 professional and SQL server 2008 R2 express

what i am trying to do is detect who is the logged in user and display their detail in a gridview. the logged in user has used asp.net authentication, i have allowed users to access the page using the login process, they have the "user" role set to allow. the admin account can view all users in the account from a gridview, but i also want a logged in user to see their own details in the members section.

this is shown in the code-behind

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
Session("MemberDetails") = User.Identity.Name
End Sub

this is in the main page

<asp:Gridview>
<Columns>
<asp:BoundField DataField="username" HeaderText="username" />
<$ many more fields here--$>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT username, salutation, fname, sname, address, suburb, postcode, dayphone FROM member WHERE (username = @username)">
<SelectParameters>
<asp:SessionParameter Name="username" SessionField="MemberDetails" />
</SelectParameters>
</asp:SqlDataSource>

all of it should work but instead i get a blank page.

Your GridView tag is not complete, especially DataSourceID="SqlDataSource1"

<asp:Gridview runat="server" ID="gvUsers" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="username" HeaderText="username" />
<$ many more fields here--$>
</Columns>
</asp:GridView>

this vb code-behind fixed my problem:

Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
        e.Command.Parameters(0).Value = Me.User.Identity.Name
    End Sub

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM