ASP.NET 2.0 ProfessionalIf 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 .
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:
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?
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.
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?
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.
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:
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>
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)
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?
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)
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?