Simplest Method to Implement a Custom Profile Provider

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karlr
    New Member
    • Feb 2007
    • 5

    Simplest Method to Implement a Custom Profile Provider

    OK, I know this has been discussed in multiple posts. However, I still have not seen a good example.

    That is, I have a custom Membership provider using the ASP.NET 2.0 controls. I am restricted to using an exising SQL 2000 database, which works for the basic fields (username, pw, email, etc). However, I need to add more information (phone number, age, etc).

    In reading the existing discussions, I can see the valid reasons for storing this information in a profile. I have downloaded and tried to use the MS Access Provider sample, with little success (probably because it is in C, and I only really know VB). I am struggling to get the basics working, such as defining a location to store the profile information (profile provider?) and how to correctly associate my existing membership data with this new profile data. I think I should link it via a UserID or something similar, but I am unable to figure out how to do this via code.

    I would greatly appreciate it if anyone could give me any guidance to a VB-specific example of how to do this or anything else that might help me. Thank you!
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    moving you to a different forum where you might get a better response

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      Originally posted by karlr
      I am struggling to get the basics working, such as defining a location to store the profile information (profile provider?) and how to correctly associate my existing membership data with this new profile data. I think I should link it via a UserID or something similar, but I am unable to figure out how to do this via code.

      I would greatly appreciate it if anyone could give me any guidance to a VB-specific example of how to do this or anything else that might help me. Thank you!
      It's easiest if you can store them as different tables in the same database, then you can open them with a single relational query using the same syntax you had in the asp file you inherited. If the two tables are called "members" and "profiles", and the field names that match are "userID" and "memberNum" then the query looks like this:
      Code:
      SELECT * FROM members, profiles WHERE userID = memberNum
      A lot of people like to give the matching fields matching names, but then you need to phrase the query like this:
      Code:
      SELECT * FROM members, profiles WHERE members.userID = profiles.userID
      and I think the first way is better.

      Is that enough for you to go on, or do you need more?

      Comment

      • karlr
        New Member
        • Feb 2007
        • 5

        #4
        jhardman,

        First off, Thank You very much for your reply. You helped clear up how to setup & query the tables in the database. However, I need more help on the front-end (VB/ASP.NET) portion.

        As I understand it, I have to define a profile provider and then add the fields that the profile will contain. I added these to my web.config file, which worked (that is, no errors). But I still could not access the fields of the profile in my code. I have seen examples where they did something like
        profile.Age = "23"
        profile.Zip = "34243"
        And the intellisense in VS picked these up in the demo projects I downloaded. In my project I kept getting errors & intellisense didn't function - as if the references in the web.config file did not exist. I am not on my development machine right now, so I can't give an exact post of what my code looks like right now - if posting it would help debug, please let me know & I will repost.

        I have been having a hard time finding a walkthrough or example where someone added a profile provider to an exising (or brand-new) project. I would GREATLY appreciate any additional guidance or information you could give me.

        Thank you again.

        Comment

        • jhardman
          Recognized Expert Specialist
          • Jan 2007
          • 3405

          #5
          Originally posted by karlr
          However, I need more help on the front-end (VB/ASP.NET) portion.
          I just code in notepad, but I'll help if I can.
          Originally posted by karlr
          As I understand it, I have to define a profile provider and then add the fields that the profile will contain. I added these to my web.config file, which worked (that is, no errors). But I still could not access the fields of the profile in my code. I have seen examples where they did something like
          I've never used it, but I thought a web.config file just stored a reference for something that had to already exist. I thought you would need to also go into, say sql server, and add a new table in your database with the fields in question. There are SQL statements for adding a field to a table, but I have never bothered with that, and I don't remember what they are. I don't think there is a statement for adding a new table. Anyone else reading this know for sure?
          Originally posted by karlr
          I am not on my development machine right now, so I can't give an exact post of what my code looks like right now - if posting it would help debug, please let me know & I will repost.
          Yeah, that might help.

          Comment

          • karlr
            New Member
            • Feb 2007
            • 5

            #6
            Thanks again for your feedback. From the examples I have downloaded, I know how to structure the SQL tables (the field names, types, etc), so I think I am OK there. I am just having a hard time "wiring up" my application to use the table that contains the profile information via ASP.NET.

            Hopefully someone can direct me to a good example of how to do this.

            P.S. It's impressive that you can code in notepad. I code in a pretty expensive version of VS2005, and my code still doesn't work. ;-)

            Comment

            • karlr
              New Member
              • Feb 2007
              • 5

              #7
              As I mentioned earlier, this is what my web.config file looks like:

              Code:
                    <profile enabled="true" defaultProvider="MyProfileProvider" >
                      <providers>
                        <add name="MyProfileProvider" type="MyProfileProvider"
                            connectionStringName="Data Source=karlweb;Initial Catalog=WebReport;Persist Security Info=True;User ID=VbUser;Password=VbUser" providername="System.Data.SqlClient" />
                      </providers>
                      <properties>
                        <add name="Country" type="string"/>
                        <add name="Gender" type="string"/>
                        <add name="Age" type="Int32"/>
                      </properties>
                    </profile>
              Maybe someone can tell me if this is valid. If so, I can start figuring out where in VB I am going wrong. Thank you.

              Comment

              • jhardman
                Recognized Expert Specialist
                • Jan 2007
                • 3405

                #8
                I feel like the answer is in the code that sends the query, not the web.config (but I could be wrong).

                Try a really simple script to see if it pulls up the right data:
                Code:
                <%
                dim connect, objRS, query
                set connect = server.createobject("ADODB.connection")
                connect.open "DSN=mydbDSN"
                set objRS = server.createobject("adodb.recordset")
                query = "SELECT * FROM [table1]"
                objRS.open query, connect, adopenstatic
                %>
                <table><tr>
                
                <%
                dim fld
                for each fld in objRS.fields %>
                   <th><%=fld.name%></th>
                <%
                next %>
                </tr><tr>
                <%
                dim idnum
                do while not objRS.eof
                   for each fld in objRS.fields %>
                      <td valign="top"><%=fld.value%></td>
                    <%
                    next
                    response.write "</tr><tr>" & vbcrlf
                    objRS.movenext
                loop %>
                this should list every field in the table you ask for. Try it for both tables, and see if it pulls up your test data.

                Comment

                • MannyZanny
                  New Member
                  • Mar 2007
                  • 2

                  #9
                  I'm not sure if this is too late for but I was searching for the same thing and found a solution and remebered your post so I signed up to help you out.

                  First, I tried your web.config text and I got it show up in intellisense after saving everything and I did a build to get it to show up (CTRL+SHIFT+B) in VS2005. It was a little tricky to get intellisense to pop it up but I'm sure if you do a build it should fix things. I also wanted to let you know you can create a group of fields like this:

                  <group name="Address">
                  <add name="Street"/>
                  <add name="Country"/>
                  <add name="PostalCod e"/>
                  </group>

                  which will allow you to access fields like this: Address.Street = "Name" Address.Country = "Country"

                  Now to actually add this information you can create controls yourself and use this code:

                  Code:
                  ' Variable to retrieve the creating status
                          Dim status As MembershipCreateStatus = Nothing
                  
                          ' Create the user
                          Dim user As MembershipUser = Membership.CreateUser("Username", "Password", "Email", "Question", "Answer", True, status)
                  
                          ' Check for errors one way by seeing if the user is nothing
                          If user Is Nothing Then
                              ' Error creating user
                              lblMessage.Text = status.ToString
                  
                              Return
                          End If
                  
                          ' Also this way by seeing the status 
                          If status = MembershipCreateStatus.Success Then
                              ' Error creating user
                              lblMessage.Text = status.ToString
                  
                              Return
                          End If
                  
                          ' Retrieve the profile of the user added
                          Dim pc As ProfileCommon = Profile.GetProfile(user.UserName)
                  
                          ' Set our custom data and save
                          pc.FullName = "Manuel Marquez"
                          pc.DOB = Now.Date ' Haha
                          pc.Address.Street = "Street Name"
                          pc.Address.Country = "Canada"
                          pc.Address.PostalCode = "H0H0H0" ' Santa Claus
                  
                          ' Save our new data
                          pc.Save()
                  
                          ' Display some message
                          Me.lblMessage.Text = "User created successfully!"
                  of course replacing string fields with textboxes, date pickers ect.

                  However if you want to use the Create User Wizard I figured it out after a couple of hours because the controls are hard to get at, don't know why they don't show up under Me?

                  Here's what I did to get it working:

                  1. Add a create user wizard (mine is CreateUserWizar d1)
                  2. Click arrow in top right then select customize create user step
                  3. Add a new fields (FullName textbox)
                  4. Now you have to rename trhe step ID by clicking "Add/Remove Wizard steps". The first one should be create user.... and change the name of the ID (last property). I chose CreateUserWizar dStep1
                  5. Add code to the create wizard CreatingUser method

                  Code:
                  Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles CreateUserWizard1.CreatingUser
                          ' Get the new user profile
                          Dim pc As ProfileCommon = Profile.GetProfile(Me.CreateUserWizard1.UserName)
                  
                          ' Get the textbox with the data in it being FullName textbox. The CreateUserStepContainer was where I found my control
                          Dim txt As TextBox = Me.CreateUserWizardStep1.FindControl("CreateUserStepContainer").FindControl("FullName")
                  
                          ' Set the field
                          pc.FullName = txt.Text
                  
                          ' Save the profile
                          pc.Save()
                      End Sub
                  Now when you add a user its created with all the extra fields added. If your wondering where I got CreateUserStepC ontainer I had to get all the controls and display the name narrowing my search until I found it using.

                  Code:
                  lblText.Text = ""
                              For Each ctl As Control In Me.CreateUserWizardStep1.Controls
                                  lblText.Text &= ctl.ID
                              Next
                  Like I said the hard part is getting the controls in order to get the entered data. This should solve your problem but be aware that the data will not go into a table nicely because the extra fields are actually saved as follows in the table aspnet_Profile:

                  Property Name Field: FullName:S:0:14 :Address.Street :S:14:9:Address .Country:S:23:6 :Address.Postal Code:S:29:7:DOB :S:36:87:

                  Property Value Field: Manuel MarquezStreetNa meCanadaH0H0H0< ?xml version="1.0" encoding="utf-16"?>
                  <dateTime>200 7-03-06T00:00:00-05:00</dateTime>


                  So if you want to display the data in a table you will have to load the profile data into a dataset manually or something. You will also find other useful methods in Membership like DeleteUser which you should be able to figure out how to use by intellisense. WOW thats a lot of information and I discovered a lot to do something simple, hope it helps you out.

                  Comment

                  • karlr
                    New Member
                    • Feb 2007
                    • 5

                    #10
                    MannyZanny,

                    Thanks a lot for researching & testing this for me! I have a couple of questions about how you did this.

                    Did you use a ASP.NET Web APPLICATION or a Web SITE? I have found that when I create a brand new ASP.NET Web Site, the intellisense for the added profile properties works properly, but if I do the exact same thing in a web Application, it does not.

                    Also, when I aded your VB code, I ran into an issue I saw earlier. That is, VS says that for this line:
                    Dim pc As ProfileCommon = Profile.GetProf ile(user.UserNa me)

                    "Type 'ProfileCommon' is not defined". Did you have to add a reference or something else to get this to work? I tried using what VS suggested as fixes ('ProfileModule ' & 'ProfileInfo') and got errors downstream.

                    Lastly, would you be willing to e-mail me your test project? Even if it is "broken" because I don't have the same database objects, I think it might be valuable to me. In any case, I will continue to test further with the methods you have kindly shown.

                    Again, thank you very much for your reply.

                    Comment

                    • MannyZanny
                      New Member
                      • Mar 2007
                      • 2

                      #11
                      I used a website but it is in my website that I'm building so I don't want to send that but I will make a small project for you to learn from. What do you want, website or web application? Not sure how to do it in web application, never made one before but I can check it out. you can send me your email to manny_zanny[at]hotmail[dot]com it's my "junk email" but i'll look for a message from you, make the subject ASP.NET Website or something that stands out.

                      Comment

                      Working...