The last few weeks I have been looking at capturing additional user information to the default username, password and email combination of the default SqlProfileProvider login process. The process of creating a simple login account and storing it with SqlProfileProvider is so simple and better documented that I wont go into this.
This is a brief introduction on how to use the inbuilt profile functionality of .net to store additional profile information
In a nutshell it is very easy to add additional fields to capture additional information in 3 easy steps.
1)Add a few lines of code into the web.config file
- <connectionStrings>
- <add name=”ApplicationServices” connectionString=”packet size=4096;Data Source=sqlserver;Initial Catalog=database;Persist Security Info=True;User ID=sa;Password=password;Max Pool Size = 500;Connection Timeout = 60″ providerName=”System.Data.SqlClient”/>
- </connectionStrings>
This sets the connection string to the database where the profile will be stored
- <profile enabled=”true”>
- <providers>
- <clear/>
- <add name=”AspNetSqlProfileProvider” type=”System.Web.Profile.SqlProfileProvider” connectionStringName=”ApplicationServices” applicationName=”/”/>
- </providers>
This enables profiles and selects the connection string to use.
- <properties>
- <add name=”FirstName” type=”String”/>
- <add name=”Surname” type=”String”/>
- <add name=”Location” type=”String”/>
- </properties>
- </profile>
The properties section contains the fields that are required to store the additional information against the users profile.
2) After you have dragged a createuserwizard from the toolbox onto a page convert to a template. Just edit the template and add the additional fields:-
- <tr>
- <td align=”right” style=”height: 26px”>
- <asp:Label ID=”FirstNameLabel” runat=”server” AssociatedControlID=”Location”>First Name:</asp:Label>
- </td>
- <td style=”height: 26px”>
- <asp:TextBox ID=”FirstName” runat=”server”></asp:TextBox>
- <asp:RequiredFieldValidator ID=”FirstNameRequired” runat=”server”
- ControlToValidate=”FirstName” ErrorMessage=”First Name is required.” ToolTip=”First Name is required.” ValidationGroup=”CreateUserWizard1″>*
- </asp:RequiredFieldValidator>
- </td>
- </tr>
I have only shown 1 additional field in the sample above.
3)We have defined were to store the profile and how to capture the additional fields. We just need to put the data in the database. In the code behind file for the create user file:-
- Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser
- Dim myProfile As ProfileCommon = CType(ProfileCommon.Create(CreateUserWizard1.UserName, True), ProfileCommon)
- ‘ custom user registration fields
- myProfile.FirstName = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(“FirstName”), TextBox).Text
- myProfile.Surname = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(“Surname”), TextBox).Text
- myProfile.Location = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl(“Location”), TextBox).Text
- myProfile.Save()
- End Sub
When a user is created it will now create the user account in the aspnet_Users table and the additional profile information in the aspnet_Profiles tables.
All tables that are prefixed aspnet_ are system generated tables.
My definition has used a single page to register the user and gather the additional information at the same time.
The createuserwizard is a good tool and the process can be spread over several pages/steps.










Recent Comments