get current logged in user asp

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Refael AckermannCommented:

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

Open in new window

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

leap29Author Commented:
hi im now getting the following compilation error??

Use of unassigned local variable 'cusername'
Just change the first line.

string cusername = string.Empty;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.