By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

Simplest Method to Implement a Custom Profile Provider

karlr
P: 5
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!
Feb 19 '07 #1
Share this Question
Share on Google+
10 Replies


100+
P: 1,646
moving you to a different forum where you might get a better response
Feb 20 '07 #2

jhardman
Expert 2.5K+
P: 3,405
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM members, profiles WHERE userID = memberNum
  2.  
A lot of people like to give the matching fields matching names, but then you need to phrase the query like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM members, profiles WHERE members.userID = profiles.userID
  2.  
and I think the first way is better.

Is that enough for you to go on, or do you need more?
Feb 20 '07 #3

karlr
P: 5
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.
Feb 21 '07 #4

jhardman
Expert 2.5K+
P: 3,405
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.
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?
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.
Feb 21 '07 #5

karlr
P: 5
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. ;-)
Feb 21 '07 #6

karlr
P: 5
As I mentioned earlier, this is what my web.config file looks like:

Expand|Select|Wrap|Line Numbers
  1.       <profile enabled="true" defaultProvider="MyProfileProvider" >
  2.         <providers>
  3.           <add name="MyProfileProvider" type="MyProfileProvider"
  4.               connectionStringName="Data Source=karlweb;Initial Catalog=WebReport;Persist Security Info=True;User ID=VbUser;Password=VbUser" providername="System.Data.SqlClient" />
  5.         </providers>
  6.         <properties>
  7.           <add name="Country" type="string"/>
  8.           <add name="Gender" type="string"/>
  9.           <add name="Age" type="Int32"/>
  10.         </properties>
  11.       </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.
Feb 21 '07 #7

jhardman
Expert 2.5K+
P: 3,405
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:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. dim connect, objRS, query
  3. set connect = server.createobject("ADODB.connection")
  4. connect.open "DSN=mydbDSN"
  5. set objRS = server.createobject("adodb.recordset")
  6. query = "SELECT * FROM [table1]"
  7. objRS.open query, connect, adopenstatic
  8. %>
  9. <table><tr>
  10.  
  11. <%
  12. dim fld
  13. for each fld in objRS.fields %>
  14.    <th><%=fld.name%></th>
  15. <%
  16. next %>
  17. </tr><tr>
  18. <%
  19. dim idnum
  20. do while not objRS.eof
  21.    for each fld in objRS.fields %>
  22.       <td valign="top"><%=fld.value%></td>
  23.     <%
  24.     next
  25.     response.write "</tr><tr>" & vbcrlf
  26.     objRS.movenext
  27. loop %>
  28.  
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.
Feb 26 '07 #8

P: 2
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="PostalCode"/>
</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:

Expand|Select|Wrap|Line Numbers
  1. ' Variable to retrieve the creating status
  2.         Dim status As MembershipCreateStatus = Nothing
  3.  
  4.         ' Create the user
  5.         Dim user As MembershipUser = Membership.CreateUser("Username", "Password", "Email", "Question", "Answer", True, status)
  6.  
  7.         ' Check for errors one way by seeing if the user is nothing
  8.         If user Is Nothing Then
  9.             ' Error creating user
  10.             lblMessage.Text = status.ToString
  11.  
  12.             Return
  13.         End If
  14.  
  15.         ' Also this way by seeing the status 
  16.         If status = MembershipCreateStatus.Success Then
  17.             ' Error creating user
  18.             lblMessage.Text = status.ToString
  19.  
  20.             Return
  21.         End If
  22.  
  23.         ' Retrieve the profile of the user added
  24.         Dim pc As ProfileCommon = Profile.GetProfile(user.UserName)
  25.  
  26.         ' Set our custom data and save
  27.         pc.FullName = "Manuel Marquez"
  28.         pc.DOB = Now.Date ' Haha
  29.         pc.Address.Street = "Street Name"
  30.         pc.Address.Country = "Canada"
  31.         pc.Address.PostalCode = "H0H0H0" ' Santa Claus
  32.  
  33.         ' Save our new data
  34.         pc.Save()
  35.  
  36.         ' Display some message
  37.         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 CreateUserWizard1)
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 CreateUserWizardStep1
5. Add code to the create wizard CreatingUser method

Expand|Select|Wrap|Line Numbers
  1. Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles CreateUserWizard1.CreatingUser
  2.         ' Get the new user profile
  3.         Dim pc As ProfileCommon = Profile.GetProfile(Me.CreateUserWizard1.UserName)
  4.  
  5.         ' Get the textbox with the data in it being FullName textbox. The CreateUserStepContainer was where I found my control
  6.         Dim txt As TextBox = Me.CreateUserWizardStep1.FindControl("CreateUserStepContainer").FindControl("FullName")
  7.  
  8.         ' Set the field
  9.         pc.FullName = txt.Text
  10.  
  11.         ' Save the profile
  12.         pc.Save()
  13.     End Sub
Now when you add a user its created with all the extra fields added. If your wondering where I got CreateUserStepContainer I had to get all the controls and display the name narrowing my search until I found it using.

Expand|Select|Wrap|Line Numbers
  1. lblText.Text = ""
  2.             For Each ctl As Control In Me.CreateUserWizardStep1.Controls
  3.                 lblText.Text &= ctl.ID
  4.             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.Coun try:S:23:6:Address.PostalCode:S:29:7:DOB:S:36:87:

Property Value Field: Manuel MarquezStreetNameCanadaH0H0H0<?xml version="1.0" encoding="utf-16"?>
<dateTime>2007-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.
Mar 7 '07 #9

karlr
P: 5
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.GetProfile(user.UserName)

"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.
Mar 7 '07 #10

P: 2
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.
Mar 8 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.