Commonly, a login username is the individual’s email address. If you have used the CreateUserWizard in ASP.NET 3.5, you know that the username field and the email field are separate fields. Username is required by the wizard and email is not. If you want to populate the email field in the database with the username field, it is not obvious on the surface how to do it. Fortunately, it’s just some minor mods to the web.config and the aspx page.
web.config
In the web.config file, you will add requiresUniqueEmail=”true” to the membership providers add entry. Below is a stripped down example.
<membership defaultProvider="MyMembership">
<providers>
<add name="MyMembership" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MyConnectionString" requiresUniqueEmail="true" />
</providers>
</membership>
aspx page
On the aspx page itself, you will add RequireEmail=”false” so the email textbox is not mandatory, a call to a function in the OnCreatedUser event, and a call to a function in the OnCreatingUser event. Also, some validation is added to the username field to ensure that it is in a valid email format. Finally, you will add an invisible textbox control for the email field since unique email is required in the webconfig.
<asp:CreateUserWizard ID="CreateUserWizard1" OnCreatedUser="CreateUserWizard1_CreatedUser" OnCreatingUser="CreateUserWizard1_CreatingUser" RequireEmail="false" Runat="server"> <asp:TextBox ID="UserName" Width="200" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="E-mail is required."ValidationGroup="CreateUserWizard1" /> <asp:RegularExpressionValidator ID="regEmail" ControlToValidate="UserName" Text="Invalid e-mail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Runat="server" /> <asp:TextBox ID="Email" runat="server" Visible="false"></asp:TextBox>
The OnCreatingUser event fires during the creation of the user and sets the invisible email text field in the CreateUserWizard to the username. This ensures that the requireUniqueEmail setting in the webconfig checks to make sure the email is unique in the database.
Protected Sub CreateUserWizard1_CreatingUser(ByVal sender As Object, ByVal e As LoginCancelEventArgs)
Dim cuw As CreateUserWizard = CType(sender, CreateUserWizard)
cuw.Email = cuw.UserName
End Sub
The OnCreatedUser event fires right after the user is created and updates the user object email field with the username.
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs)
Dim userNameTextBox As TextBox = CType(CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"), TextBox)
Dim user As MembershipUser = Membership.GetUser(userNameTextBox.Text)
user.Email = user.UserName
Membership.UpdateUser(user)
End Sub
No related posts.


Twitter
About
8 Comments
Thanks just what I was looking for.
Gary
hi thanks a lot. works great. my email on the profile is blank. can you please post the code so that the email is stored at the email field too?
thanks
@nis
Just updated the post. Run through it one more time and see if that takes care of it for you.
Jen, I have tried every solution I can find to plug email into username to no avail. I am using C# but you are the only one using two event handlers; one for CreaingUser and one for CreatedUser. Do you happen to know if this is the case with C# also? If so then maybe I need to follow your example.
The CreatingUser puts a value in the email field which is required for the wizard. The CreatedUser assigns that value to the email field in the database.
I am getting an SQL Exception Error:
Violation of PRIMARY KEY constraint 'UserProfile_PrimaryKey'. Cannot insert duplicate key in object 'dbo.UserProfile'.
The statement has been terminated
I am storing additional fields in a seperate UserProfile table with a Primary Key UserId and a Foreign Key linked to the UserId Parameter in the aspnet_Users. The strange thing is that the record is added successfully in all the three tables. aspnet_Membership, aspnet_Users, and UserProfile tables.
it stops at
myCommand.ExecuteNonQuery()
Hi Jens,
Thanks, your blog was most useful. Is there a way to let the user know that the email address he entered already exists in the database?
Thanks for your help,
Anthony
@Anthony
See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createusererroreventargs.createusererror.aspx