Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 7th, 2006, 08:37 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating custom properties using CreateUserWizard

Hi,

I'm trying to add custom properties to my user profiles, i've created a set of profiles:

<profile enabled ="true">
            <properties>
                <add name="MobileNumber"/>
                <add name="Gender" type ="string" />
                <add name="Age" type ="integer" />
            </properties>
        </profile>

And i've edited the contents of the create user wizard to have three more textboxes with ids of MobileNumber, Gender and Age.

Most examples i've seen just tell me to put in the CreateUserWizard1_CreatedUser method a simple line saying

Profile.MobileNumber = MobileNumber.Text, however this does not work as it says MobileNumber is not declared but it is.

so I have tried this instead:

Profile.MobileNumber = CreateUserWizard1.CreateUserStep.ContentTemplateCo ntainer.FindControl("MobileNumber")

but this does not work either and comes up with an error saying Value of type 'System.Web.UI.Control' cannot be converted to 'String'.

I'm quite new to coding so this may be a very simple problem to solve, i'd be grateful of any help anyone can give me


Reply With Quote
  #2 (permalink)  
Old March 8th, 2006, 02:28 PM
Imar's Avatar
Wrox Author
Points: 67,332, Level: 100
Points: 67,332, Level: 100 Points: 67,332, Level: 100 Points: 67,332, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,231
Thanks: 66
Thanked 1,418 Times in 1,398 Posts
Default

You're close....

What you're trying to do is good: get a reference to the control. However, there are a few problems.

MobileNumber is indeed not a control, so you cannot assign a control to it. FindControl returns a generic control, and not a string. Once you have a reference to the control, you'll need to cast it to an appropriate control (e.g. DropDownList, TextBox etc) and then access a useful property, like the SelectedValue or the Text property:

CType(CreateUserWizard1.CreateUserStep.ContentTemp lateContainer.FindControl("MobileNumber"), TextBox).Text

This looks up the MobileNumber control, casts it to a TextBox and then gets its text.

Be aware that in the CreatedUser event the Profile object for the current user hasn't been setup yet. This normally happens early in the ASP.NET pipeline, but since you're working much later in the pipeline, the profile isn't there yet. However, you can programmatically create the profile object for the user and access it like this:

Dim myProfile As ProfileCommon = CType(ProfileCommon.Create(CreateUserWizard1.UserN ame, True), ProfileCommon)

The you can set the properties:

myProfile.MobileNumber = Mobile Number from earlier code.

You only need to do this in the page with the CreateUserWizard. Subsequent pages will have the correct Profile set up.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Why Go? by Faithless (Track 6 from the album: Sunday 8PM) What's This?
Reply With Quote
  #3 (permalink)  
Old March 8th, 2006, 03:02 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks Imar what you said made much more sense than the hundreds of websites i've looked at!

However when i put in your suggestion it switched back to the previous error "System.NullReferenceException: Object reference not set to an instance of an object."

do i need to declare the control in the method such as dim MobileNumber as Integer(), i've checked all the spellings and that the id of the textboxes are the same as what i'm using but I can't seem to fix it.

Thanks

Reply With Quote
  #4 (permalink)  
Old March 8th, 2006, 03:05 PM
Imar's Avatar
Wrox Author
Points: 67,332, Level: 100
Points: 67,332, Level: 100 Points: 67,332, Level: 100 Points: 67,332, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,231
Thanks: 66
Thanked 1,418 Times in 1,398 Posts
Default

Can you post a trimmed down version of the page and its code behind?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sie by Einstürzende Neubauten (Track 5 from the album: Tabula Rasa) What's This?
Reply With Quote
  #5 (permalink)  
Old March 8th, 2006, 03:25 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the wizard on Login.aspx and an example of the mobile number textbox without validation.

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" LoginCreatedUser = "true" OnCreatedUser="CreateUserWizard1_CreatedUser">
<WizardSteps>
 <asp:CreateUserWizardStep runat="server">
 <ContentTemplate>
 <tr>
    <td align="right" style="height: 26px">
    <asp:Label ID="MobileNumberLabel" runat="server" AssociatedControlID="MobileNumber">Mobile Number:</asp:Label>
   </td>
   <td style="height: 26px">
   <asp:TextBox ID="MobileNumber" runat="server"></asp:TextBox>
     </td>
 </tr>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server">
</asp:CompleteWizardStep>
</WizardSteps>
    </asp:CreateUserWizard>

This is the login.aspx.vb page:

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser


        Dim myProfile As ProfileCommon = CType(ProfileCommon.Create(CreateUserWizard1.UserN ame, True), ProfileCommon)


 myProfile.MobileNumber = MobileNumber(CType(CreateUserWizard1.CreateUserSte p.ContentTemplateContainer.FindControl("MobileNumb er"), TextBox).Text)

myProfile.Save()

End Sub
End Class

And this is the profile information from the web.config

                <profile enabled ="true">
            <properties>
        <add name="MobileNumber" type ="integer" />
        <add name="Gender" type ="string" />
        <add name="Age" type ="integer" />
            </properties>
        </profile>

I keep reading about custom providers, do i need to create my own or do this a whole different way? All i want to do is allow the user to register giving three extra pieces of information which will be stored and then the mobile number used for sending information to the user at a later date.

Reply With Quote
  #6 (permalink)  
Old March 8th, 2006, 05:00 PM
Imar's Avatar
Wrox Author
Points: 67,332, Level: 100
Points: 67,332, Level: 100 Points: 67,332, Level: 100 Points: 67,332, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,231
Thanks: 66
Thanked 1,418 Times in 1,398 Posts
Default

Is this all the code for the CreateUserWizard?

If so, you should be getting other errors, because you don't have the username and other controls on it.

If you have them, but didn't post them, remove the MobileNumber from the profile code. I think you looked at C# code where you cast by doing something like this:

(MyStronglyTypedObject)SomeObject

to turn SomeObject into MyStronglyTypedObject.

However, VB.NET uses CType for that. Besides, you can't cast to MobileNumber, because that's not a valid class. Instead, the number is an int (not a very good choice though), so you should cast / convert the Text property to an Integer:

myProfile.MobileNumber = Convert.ToInt32(myTextbox.Text)

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #7 (permalink)  
Old March 8th, 2006, 05:24 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again,

i'm not sure I understand what you mean, are you saying that i don't need to put the MobileNumber in the profile in web.config? if so where would i put it instead as i need it to be added. Also what type would you suggest that the variable be if not an integer? It seems that whatever way i try to do myProfile.MobileNumber = MobileNumber.text it always says that MobileNumber is not declared.even if i tried to cast it or convert it in some way. is there something i need to put at the top that the page inherits or a certain class that it needs.

All the pages i've seen on this topic state that all i would need to do is add the variable in the profile in webconfig, add a textbox of the variable in the userwizard and then put profile.variable = variable.text but it just doesn't work for me.

Sorry for being annoying, i've posted the full userwizard below if that is any help

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" LoginCreatedUser = "true" OnCreatedUser="CreateUserWizard1_CreatedUser">
        <WizardSteps>
            <asp:CreateUserWizardStep runat="server">
                <ContentTemplate>
                    <table border="0">
                        <tr>
                            <td align="center" colspan="2">
                                Sign Up for Your New Account</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server"></asp:TextBox>

                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>

                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                        </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                                                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                                           </td>
                        </tr>
                        <tr>
                            <td align="right" style="height: 26px">
                                <asp:Label ID="MobileNumberLabel" runat="server" AssociatedControlID="MobileNumber">Mobile Number:</asp:Label></td>
                            <td style="height: 26px">
                                <asp:TextBox ID="MobileNumber" runat="server"></asp:TextBox>
                                                           </td>
                        </tr>

                       <tr>
                           <td align="right" style="height: 26px">
                                <asp:Label ID="AgeLabel" runat="server" AssociatedControlID="Age">Age:</asp:Label></td>
                            <td style="height: 26px">
                                <asp:TextBox ID="Age" runat="server"></asp:TextBox>
                                                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                Gender</td>
                            <td>
                                <asp:TextBox ID="Gender" runat="server"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                    ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                    ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color: red">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:CreateUserWizardStep>
            <asp:CompleteWizardStep runat="server">
            </asp:CompleteWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>

Reply With Quote
  #8 (permalink)  
Old March 8th, 2006, 06:31 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I got it to compile by doing it but which table does the mobilenumber go into or do i need to specify it somehow as i can't find it where the data has been inserted. Do you need to specify where the new data would go in the membership tables?

this is what i did

Dim Profile As ProfileCommon = CType(ProfileCommon.Create(CreateUserWizard1.UserN ame, True), ProfileCommon)


        Dim mytextbox As TextBox

        mytextbox = CType(CreateUserWizard1.CreateUserStep.ContentTemp lateContainer.FindControl("MobileNumber"), TextBox)

        Profile.MobileNumber = Convert.ToInt32(mytextbox)


Reply With Quote
  #9 (permalink)  
Old March 9th, 2006, 12:04 PM
Authorized User
 
Join Date: Feb 2006
Location: , , .
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This might be a stupid suggestion but could i not just add an extra column called mobilenumber to the membership column and create a insert statement to insert the contents of the mobile number textbox when the user is created?

Reply With Quote
  #10 (permalink)  
Old March 9th, 2006, 04:31 PM
Imar's Avatar
Wrox Author
Points: 67,332, Level: 100
Points: 67,332, Level: 100 Points: 67,332, Level: 100 Points: 67,332, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,231
Thanks: 66
Thanked 1,418 Times in 1,398 Posts
Default

Hi again,

Sorry for my late response... Was kinda busy. Anyway, I'll try to address your questions / concerns:
Quote:
quote:I keep reading about custom providers, do i need to create my own or do this a whole different way? All i want to do is allow the user to register giving three extra pieces of information which will be stored and then the mobile number used for sending information to the user at a later date.
You could, but you don't need it. Basically, you only need to build a custom provider if you want to target a different data source or schema. So, if you have existing users in a SQL Server database with its own schema, or want to target, say, Oracle, then you need to write a custom provider.
From what I can see, your requirements don't need this; the default membership, in combination with the Profiles feature should be enough.
Quote:
quote:i'm not sure I understand what you mean, are you saying that i don't need to put the MobileNumber in the profile in web.config? if so where would i put it instead as i need it to be added.
You do need to put in the Web.config file, or otherwise it won't be available in the Profile. What happens is that when you add a property in the config file, .NET will compile a class on the fly with the properties in the config file. That's why you can have code like this: Profile.MobileNumber and get Intelli Sense helping you with the code.
Quote:
quote:Also what type would you suggest that the variable be if not an integer?
It's unlikely you'll want to do calculations with the Mobile Number, so a String is much more logical. What do you do when people add something like +44 (43) 38378346?? That may be a valid number, but not a valid Integer.  
Quote:
quote:It seems that whatever way i try to do myProfile.MobileNumber = MobileNumber.text it always says that MobileNumber is not declared.even if i tried to cast it or convert it in some way. is there something i need to put at the top that the page inherits or a certain class that it needs.
Does it say that on the property you're trying to access? (e.g. txtMobileNumer.Text) or on the Profile property (e.g. myProfile.MobileNumber)?

In the first case, this happens when you try to access something like txtMobileNumber directly. You say you have it declared, and you're right. However, it's not declared at the Page level, so you can't access it directly. Instead, you'll need to use the code you posted earlier, and that I fixed in a previous post. That is, use FindControl on the CreateUserWizard and then cast it to a TextBox and access the Text property.
Quote:
quote:All the pages i've seen on this topic state that all i would need to do is add the variable in the profile in webconfig, add a textbox of the variable in the userwizard and then put profile.variable = variable.text but it just doesn't work for me.
Almost; that's how it normally works in other pages. However, because you're using a CreateUserWizard control you need to "find" the MobileNumber text box control and get its text.
Quote:
quote:I got it to compile by doing it but which table does the mobilenumber go into or do i need to specify it somehow as i can't find it where the data has been inserted. Do you need to specify where the new data would go in the membership tables?
The additional properties are not stored in a separate column in the Membership user table. So, you won't find a MobileNumber column in that table. Instead, the info is stored in aspnet_Profiles. This table has a number of columns. One column holds the list of available properties and their length. An other holds the entire profile data in quite an awkward format. You won't be able to see how the data is stored directly; you'll need the other column to understand the format.
Quote:
quote:This might be a stupid suggestion but could i not just add an extra column called mobilenumber to the membership column and create a insert statement to insert the contents of the mobile number textbox when the user is created?
I have been taught there are no stupid questions; only stupid answers... ;) I'll try no to supply one...
You could do that, but it's not worth the trouble. You'd need to create a class that inherits from MembershipUser and add additional properties. However, this brings a few problems of its own. First, you'd need to implement behavior to save the additional properties your class gets. Second, you'd need to cast the MembershipUser to your MemberShipUser class wherever you need to access the additional properties. Again, in your situation you don't need all of this.

On my own website, I have implemented what I described here: used the standard CreateUserWizard control, customized the template and added a bunch of properties for address data, like city and country. That data is stored in the Profile whenever someone signs up. Works fine, with exactly the code I posted and described earlier. You can see for your self by signing up for an account (just follow the What's This? link at the bottom of this post and then click the Login tab on my website)

Scott Guthrie has pasted a nice blog post about this, including sample code. You can find the item here: How to add a Login, Roles and Profile system to an ASP.NET 2.0 app in only 24 lines of codeBasically, he shows you exactly what you need, with only 24 lines of hand written code....

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Thoughtless by KoRn (Track 6 from the album: Untouchables) What's This?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference Custom TextBoxes in CreateUserWizard wirerider ASP.NET 2.0 Basics 1 September 15th, 2006 10:00 PM
CreateUserWizard problem with custom step danblack101 ASP.NET 2.0 Professional 0 July 12th, 2006 01:09 AM
Populate custom properties of shape in Visio 2002 2bigfeet23 Pro Visual Basic 2005 0 April 11th, 2006 08:19 AM



All times are GMT -4. The time now is 07:12 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.