Learn how to a build a cloud-first strategyRegister Now


get current logged in user asp

Posted on 2008-11-13
Medium Priority
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

Expert Comment

ID: 22949210

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

Open in new window


Expert Comment

ID: 22949225
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

ID: 22949534
hi im now getting the following compilation error??

Use of unassigned local variable 'cusername'

Accepted Solution

OBonio earned 500 total points
ID: 22949645
Just change the first line.

string cusername = string.Empty;

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

810 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