Storing User Profile into a Custom Table using CreateUser Wizard control
I discussed how you can add extra controls to CreateUserWizard(CUW) Control in the blog post HERE.
Assuming you have gone through the post above, what we will do here is store this extra information collected in CUW into a custom database.
Other option is storing in asp.net Profiles. Check References section at the end to see how to store user information in profiles.
Add Extra Fields to CUW Control:
Lets say we want to collect FirstName and LastName of the User while creating a user.With the help of previous post add two textboxes namely FirstName and LastName. Add Validation controls accordinly.
Create a Custom Table to store User Profile(here FirstName and LastName)
-
Add a new Table to your Membership Database. Here I have named it User_Profile.
-
Add 3 columns namely UserId - type uniqueidentifier, FirstName - type varchar(50) and LastName - type varchar(50)
-
Set UserId as Primary Key
-
Create a Foreign key relationship between UserId of User_Profile table and aspnet_Users table. You can look sample example screenshots here.
So once you have your table ready to store the information lets go ahead and look at the code how to insert values into it.
Inserting FirstName and LastName into User_Profile
We will use the CreatedUser event of CUW control to do this job. In the design Mode, double click the Create User button. In the code behind for CreateUser page you will see an event handler added for CreatedUser event. Here is the code that says the rest:
C#:
{
// Get the UserId of the just-added userMembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);
Guid newUserId = (Guid)newUser.ProviderUserKey;
//Get Profile Data Entered by user in CUW controlString FirstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
String LastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;// Insert a new record into User_Profile
// Get your Connection String from the web.config. MembershipConnectionString is the name I have in my web.config
string connectionString = ConfigurationManager.ConnectionStrings["MembershipConnectionString"].ConnectionString;string insertSql = "INSERT INTO User_Profile(UserId,FirstName, LastName) VALUES(@UserId, @FirstName, @LastName)";
using (SqlConnection myConnection = new SqlConnection(connectionString)){
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@UserId", newUserId);myCommand.Parameters.AddWithValue("@FirstName", FirstName);
myCommand.Parameters.AddWithValue("@LastName", LastName);myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
VB.NET:
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser' Get the UserId of the just-added user
Dim newUser As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)
Dim newUserId As Guid = DirectCast(newUser.ProviderUserKey, Guid)
'Get Profile Data Entered by user in CUW control
Dim FirstName As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FirstName"), TextBox).Text
Dim LastName As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("LastName"), TextBox).Text
' Insert a new record into User_Profile
' Get your Connection String from the web.config. MembershipConnectionString is the name I have in my web.config
Dim connectionString As String = ConfigurationManager.ConnectionStrings("MembershipConnectionString").ConnectionString
Dim insertSql As String = "INSERT INTO User_Profile(UserId,FirstName, LastName) VALUES(@UserId, @FirstName, @LastName)"
Using myConnection As New SqlConnection(connectionString)
myConnection.Open()
Dim myCommand As New SqlCommand(insertSql, myConnection)
myCommand.Parameters.AddWithValue("@UserId", newUserId)
myCommand.Parameters.AddWithValue("@FirstName", FirstName)
myCommand.Parameters.AddWithValue("@LastName", LastName)
myCommand.ExecuteNonQuery()
myConnection.Close()
End Using
End Sub
-------------------------
Most of the code is self-explanatory. We get the UserId of the newly created user and the values in FirstName and LastName textboxes.
Then its simple sql database insertion code.
NOTE: Make sure you have --> oncreateduser="CreateUserWizard1_CreatedUser" set in your CUW Markup (aspx). If is it not set this custom CreatedUser event handler will not be executed. So the user will be created in your DB but the profile data will not be updated as the code above is not executed at all. Check HERE what I mean.
Concerns:
-
We haven't taken into consideration one scenario. What if something goes wrong between UserCreation and storing profile. i.e. User is created successfully but Profile does not get stored. May be we will discuss that in some other article.
-
You can add exception handling.
Thanks for reading.
References:
-
Storing Additional User Information: http://www.asp.net/learn/security/tutorial-08-cs.aspx
Having issues implementing above?
If you are facing any issues I would suggest you post your question here - http://forums.asp.net/25.aspx - with more details.Then post the link to that thread below in the comment. I will take a look at it as soon as I get a chance. Your chances to get prompt response increases by posting on that forum.