locked
Get the UserID of the current logged in user RRS feed

  • Question

  • User-1506996564 posted

    Hello

    I use the following code to get the current user

    Protected Sub LoginUser_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginUser.LoggedIn
            Dim currentUser As MembershipUser = Membership.GetUser(User.Identity.Name)
            Dim username As String = currentUser.UserName
    
            Dim userID As Guid = currentUser.ProviderUserKey
    
            '''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim sql As String = "SELECT CustomerID,CustomerName FROM Customer WHERE UserID = " + userID.ToString
            Dim myConnection As SqlConnection = New SqlConnection("Data Source=mssql501.ixwebhosting.com;Initial Catalog=C209256_library;User ID=C209256_library; Password=A123456789")
            Dim myCommand As SqlCommand = New SqlCommand(sql, myConnection)
            myConnection.Open()
            Dim myReader As SqlDataReader = myCommand.ExecuteReader()
    
            If myReader.Read() Then
                Session.Add("CustomerID", myReader(0))
                Session.Add("CustomerName", myReader(1))
            End If
    
    
    
            myReader.Close()
            myConnection.Close()
    
    
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
            
    
    
    
        End Sub

    but I get the following error

    Object reference not set to an instance of an object.

    any ideas please?

    thanks

    Saturday, May 3, 2014 6:11 AM

Answers

  • User-1199946673 posted

    When using the Login Control, the user will be logged in AFTER the first request. In the LoggedIn evente, User.Identity.Name will not be availlable. Instead, use LoginUser.Username

    Dim currentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 3, 2014 6:18 AM

All replies