Loupi
Loupi

Reputation: 1112

Getting current logged in user to display it records in gridview in ASP.NET C#

I'm trying to display the current logged in user and its record in gridview. If a user logged in he can view all of his records and tables associated to him. Here's a simple code of mine.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Account.aspx.cs" Inherits="LibrarySystem.Member.MyAccount" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h3>
    My Account</h3>
<p>
    &nbsp;</p>
<p>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="lenid" DataSourceID="useraccountDataSource" 
        ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:BoundField DataField="lenid" HeaderText="Lend ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="lenid" />
            <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                SortExpression="bookid" />
            <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="department" HeaderText="Department" 
                SortExpression="department" />
            <asp:BoundField DataField="dateborrowed" HeaderText="Date borrowed" 
                SortExpression="dateborrowed" />
            <asp:BoundField DataField="datereturned" HeaderText="Date returned" 
                SortExpression="datereturned" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="useraccountDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
        SelectCommand="SELECT [lenid], [bookid], [EmployeeID], [department], [dateborrowed], [datereturned] FROM [LendTable] WHERE ([EmployeeID] = @EmployeeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="EmployeeID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    </p>
<p>
    &nbsp;</p>
</asp:Content>

EmployeeID is the same as the Username (I've pointed the aspnet user database to my own database). I wanted to display logged in user's record only to the current logged in user.

Any help would be much appreciated ;) Thanks in advance.

Upvotes: 0

Views: 2511

Answers (1)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Since you said in the question that the employeeId is the Username, you can access the logged in Username as follows:

HttpContext.Current.User.Identity.Name

Upvotes: 1

Related Questions