473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Simplest Method to Implement a Custom Profile Provider

karlr
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
10 7225
willakawill
1,646 1GB
moving you to a different forum where you might get a better response
Feb 20 '07 #2
jhardman
3,406 Expert 2GB
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
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
3,406 Expert 2GB
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
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
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
3,406 Expert 2GB
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
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
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
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

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

Similar topics

2
by: | last post by:
Hi all, How can I get a reference to my custom profile provider in my .aspx page? I have looked at httpcontext.current.profile. But from there where do I go? Ideally I would like to be able to...
2
by: John | last post by:
Hi I am trying to setup a customised membership provider and I am getting the following error when trying to run the app; The entry 'AspNetSqlMembershipProvider' has already been added. The...
4
by: techsupport | last post by:
I have some experience with .NET Remoting, as well as ASP.NET 2.0, and have been wanting to remote a custom membership and profile provider. I want to take advantage of the new controls in ASP.NET...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
0
by: Phil | last post by:
I created a custom profile provider based on the example here : http://www.theserverside.net/articles/showarticle.tss?id=CreatingProfileProvider It works ok, but when I try to get the last update...
3
by: Rich Armstrong | last post by:
I've implemented a custom profile provider following the pattern and examples provided with ASP.NET 2.0, but no Profile object ever appears in my page; that is, after executing the following, ...
0
by: Rajesh | last post by:
I am trying to use a simple custom type for saving the profile information of a user. The custom class inherits from the Hashtable. I am serializing the type as "Binary" in the provider sections of...
9
by: Kirk | last post by:
I have successfully, implemented a custom Membership Provider to a SQL 2000 table, however, I am having problems doing the same with a Profile Provider. As I understand it, the steps for both of...
2
by: Kees de Winter | last post by:
Hi, I am thinking about using the personalization features of .net 2.0 but I can't figure out the following. If I use the standard features of personalization, I will have users in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.