Solved

membership.getuser() for parameter to display custom user information

Posted on 2010-11-23
15
577 Views
Last Modified: 2012-05-10
Hello,

I set up my site using a SQlMembershipProvider and Customized the CreateUserWizard to collect additional user information such as LastName, FirstName, etc.

I created a table to store the additional info instead of implementing profiles because I didn't wat to be tied to that structure.

Now I'm having trouble retrieving the additional user information to display on page in labels or text boxes, after the user is logged in.

I'm trying a 3 Tiered Architecture with business objects for the first time and I'm probably doing this wrong or making it difficult but I really trying to learn it. I'm pretty new to programming.

Please check out my attachment and give me some advice....
 Under GetOfficerInfo function, I get an error with Return reader --Name 'reader' is not declared.
Thanks MyCode.pdf
0
Comment
Question by:tis9700
  • 8
  • 6
15 Comments
 
LVL 5

Expert Comment

by:JayFromPep
ID: 34199021
The problem is that the variable 'reader' is not available to the return statement.   Create an empty reader at a higher level in the code tree and reference it in the If statement.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34199070
since reader is declared within the scope of the using statement it is not available for return.  what you should actually do is declare your officerInfo object higher up and return that since that is what the return type of the sub is
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34199085
i think this should do it
Public Shared Function GetOfficerInfo(ByVal uId As String) As OfficerInfo

        Dim errorstr As String = String.Empty

        Dim offNo As New OfficerInfo

        'get a reference to the currenly logged on user

        Dim currentUser As MembershipUser = Membership.GetUser("UserId")

        'determine the currently logged on user's UserId value

        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)

        Using conn As New SqlConnection _

        (ConfigurationManager.ConnectionStrings _

        ("connEnforceResource").ConnectionString)

            Try

                conn.Open();

                Using cmd As New SqlCommand("GetUserProfile", conn)

                    cmd.CommandType = CommandType.StoredProcedure

                    cmd.Parameters.AddWithValue("@userId", currentUserId)

                    Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)

                    If reader.Read Then

                        'offNo.UserId = rd("UserId")

                        offNo.OfficerNo = reader("OfficerNo")

                        offNo.FirstName = reader("FirstName")

                        offNo.LastName = reader("LastName")

                    End If

                End Using

            Catch ex As Exception

                errorstr += "" & ex.Message

                Return Nothing

            Finally

                CType(conn, IDisposable).Dispose()

            End Try

            Return offNo 'have a error here Name ‘reader’ is not declared.

        End Using

    End Function

Open in new window

0
 

Author Comment

by:tis9700
ID: 34200041
Thanks for responding.

Burakiewicz,

I made the chages you suggested. By the way, was conn.Open(); a typo or because you mainly use C#?

Anyway, after implemeinting your suggested changes, I  created a function in a Business Logic Layer..

Public Class OfficerBl
     Public Shared FunctiongetOfficer(ByVal uId as String) as OfficerInfo
           Return ActivityDll.GetOfficerInfo(uId)
    End Function
End Class

Then I try to display the results in three textboxes after the user logs in.....
The textboxes are actually located within a Master page....
Master Page code-behind


Public Sub LoadInfo()
      Dim displayOfficer as New OfficerInfo

      txtBadge.text = displayOfficer .OfficerNo
      txtFirstName.text = displayOfficer .FirstName
      txtLastName.text = displayOfficer .LastName
End Sub
Then in the Page_Load event...
If  Not Page.IsPostBack Then
       LoadInfo()
End If

While I have no error on compling, no values are returned.

In my data access layer, I have (Byval uId as string) , should I set that equal to currentUserId? Or something? Could that be the issue?

Thanks
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34200076
C#

In your load i dont see you calling the FunctionGetOfficer and setting that to displayOfficer
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34200141
youll need to add something like this to the load
 Dim displayOfficer As New OfficerInfo
  displayOfficer = GetOfficerInfo("Test")<--replace test with the current id you want
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34200148
actually since in the latest code its
displayOfficer = FunctiongetOfficer("UID")
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tis9700
ID: 34200357
Ok...

I added ...

displayOfficer = OfficerBl.getOfficer("uId")

And I at least get an error when I log in...within my ActivityDll
Null Reference Exception
Object reference not set to an instance of an object

Line 137: Dim currentUserId as Guid = CType(currentuser.ProviderUserKey, Guid)

So I wonder...
Am I not getting the User from  membership.getuser()?

Maybe I should have
Dim currentUser as Membership = Membership.GetUser("UserId")
Dim currentUserId as Guid = CType(currentuser.ProviderUserKey, Guid)
someplace other than the data access layer . Perhaps in my code behind?

and

cmd.Parameters.AddWithValue("@userId", uId)

Man, I am getting confused!
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34200419
displayOfficer = OfficerBl.getOfficer("uId")

"uid" needs to be replace with the actual user id you want to pass
and this
Dim currentUser as Membership = Membership.GetUser("UserId")
UserId needs to be the user id you are looking for

and to check that you are getting the right user
Dim currentUser as Membership = Membership.GetUser("UserId")
Dim currentUserId as Guid = CType(currentuser.ProviderUserKey, Guid)<-- put breakpoint and see if user is correct
0
 

Author Comment

by:tis9700
ID: 34200576
Ok

Put a breakpoint on that line

and currentUserId is returning empty
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 34200667
what does current user have when it hits the breakpoint?  currentUserId will be empty until you step passed the breakpoint
you will need to have the "UserID" be the user id you are grabbing
Dim currentUser as Membership = Membership.GetUser(Put user id here)
0
 

Author Comment

by:tis9700
ID: 34200924
Hey burakiewicz,

I have to cut off for the night. Family Thanksgiving stuff.

I'll be back in the morning. Thanks for all you help today. Hope I'm not wearing you out.

0
 

Author Comment

by:tis9700
ID: 34207670
Hey burakiewicz,

I got it working!

I moved the currentUser and currentUserId to my Presentation Layer on the Page_Load

Used HttpContext.Current.User.Identity.Name to get the currentUser and passed it to the currentUserId to get the userId

set currentUserId = currentUser.ProviderUserKey.ToString

Then passed currentUserId to getOfficer object for the OfficerBl..

I attached my revised code. Would you look at it and tell me what you think? Should I be checking the object to make sure it is not nothing? Or anything else that is best practice?

Thanks and Happy Thanksgiving!
myCode.pdf
0
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 34231209
one thing in the DAL is that you are just catching the exception and not really handling it so if there is a connection error or anything it could be hidden, so i would throw the error or handle it.  Then i would also check that display officer is something before assigning it to the textboxes
0
 

Author Comment

by:tis9700
ID: 34231404
Thanks buraiewicz,

I appreciate the help and advice you've given me.  I'll start researching how to handle exceptions.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

13 Experts available now in Live!

Get 1:1 Help Now