get current logged in user asp

Posted on 2008-11-13
Last Modified: 2012-05-05
I have the following code written in VB. Which grabs hold of the current logged in user and assigns it to a string.

I need to do something similar but in C#. I have a page which is going to populate a gridview depending on the currently logged in user. but for the life of me i cant find a away to assign the current logged in username to a variable.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim cusername As String

        If HttpContext.Current.Request.IsAuthenticated Then

            cusername = HttpContext.Current.User.Identity.Name        End If

        SqlDataSource1.SelectCommand =  select * from  aspnet_membership where email = '" & cusername & "' 

    End Sub

End Class

Open in new window

Question by:leap29
    LVL 7

    Expert Comment


    String cusername = (HttpContext.Current.Request.IsAuthenticated) ? HttpContext.Current.User.Identity.Name : null;

    Open in new window

    LVL 9

    Expert Comment

    The c# version is below. You were missing quotes at the beginning and end of the sql statement.

    You might want to check if your code is going into the if statement.

    string cusername;
            if (HttpContext.Current.Request.IsAuthenticated)
                cusername = HttpContext.Current.User.Identity.Name;
            SqlDataSource1.SelectCommand = "select * from  aspnet_membership where email = '" + cusername + "'";

    Open in new window


    Author Comment

    hi im now getting the following compilation error??

    Use of unassigned local variable 'cusername'
    LVL 8

    Accepted Solution

    Just change the first line.

    string cusername = string.Empty;

    Open in new window


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now