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:

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

{

// Get the UserId of the just-added user

MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);

Guid newUserId = (Guid)newUser.ProviderUserKey;

//Get Profile Data Entered by user in CUW control

String 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();

}

}

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:

 

Published Tuesday, January 27, 2009 5:36 PM by guru_sarkar

Comments

# Adding additional controls to CreateUserWizard (CUW) Control - Guru Sarkar's Blog

Pingback from  Adding additional controls to CreateUserWizard (CUW) Control - Guru Sarkar's Blog

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Wednesday, March 11, 2009 9:33 PM by andrethompson

Excellent! Exactly what I am looking for!

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Saturday, March 21, 2009 12:50 PM by andrethompson

Hi

If I inserted a checkbox, how do I access the state of the checkbox in the wizard?

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Saturday, March 21, 2009 11:24 PM by andrethompson

hi

I did exactly as the above, the user is created in the system aspnetdb tables but no entries are made in the user_profile table

Can you help?

athompson@cits.uwi.tt

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Sunday, March 22, 2009 10:51 PM by andrethompson

I think the wrong event handler is being used - the code does not execute - I think this code should be in the Created User event handler.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Tuesday, March 24, 2009 12:53 PM by guru_sarkar

andrethompson

Are you getting any error?

The code is already placed in CreatedUser event handler.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Sunday, June 21, 2009 5:23 AM by Hasan

I am new to programming. I have copied and pasted this code and it is giving error and it does not recognise commands like SqlConnection and SqlCommand. Any idea why?

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Monday, June 22, 2009 11:06 AM by guru_sarkar

Hasan,

You should include referenc to System.Data.SqlClient namespace like:

using System.Data.SqlClient;

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Wednesday, July 01, 2009 12:45 AM by Sevensnake77

This does not work. No one have not found a way yet.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Wednesday, July 01, 2009 2:03 PM by guru_sarkar

Sevensnake77,

If you can tell what is not working we can try making it work.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Tuesday, July 07, 2009 6:51 AM by Sabari M.D

" CreateUserWizard1: CreateUserWizardStep.ContentTemplate does not contain an IEditableTextControl with ID UserName for the username. "

This is what am facing whil running ..any solution??

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Tuesday, July 07, 2009 10:30 AM by guru_sarkar

Sabari M.D

That is because for some reason UserName TextBox is either removed or Renamed in your CreateUserWizard. Go to the SourceView of that page and you need to add a TB with ID UserName.

If you have any question please share your CUW markup code here.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Tuesday, July 07, 2009 7:56 PM by Sevensnake

My control always come back null any suggestions  

Dim usertxt As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox)

       'Dim Email As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName"), TextBox)

       Dim user As MembershipUser = Membership.GetUser(usertxt.Text)

       Dim userId As Guid = DirectCast(user.ProviderUserKey, Guid)

       ' Dim RoleId As Guid = DirectCast(RoleGroup, Guid)

       Dim RoleIDValue As DropDownList = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("DropDownList1"), DropDownList)

       'Dim RoleId As New Guid(RoleIDValue.SelectedValue)

       Dim txtfname As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtfname"), TextBox)

       Dim txtlname As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtlname"), TextBox)

       Dim MiddleNameTextBox As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Company"), TextBox)

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Tuesday, July 07, 2009 10:54 PM by sevensnake77

you mean something like this.

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Wednesday, July 08, 2009 7:14 PM by sevensnake77

ok I started debuging, I see the text i entered in the box. But it does not insert into the database.  and no errors.

"address11"

TemplateSourceDirectory "/" String

Text "address here" String

TextMode SingleLine {0} System.Web.UI.WebControls.TextBoxMode

ToolTip "" String

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Wednesday, July 08, 2009 8:39 PM by sevensnake77

I got it. And I did not have to use next button, everything in one.

Dim fname As TextBox = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtfname"), TextBox)

cmdInsert.Parameters.Add(New SqlParameter("@fname", Data.SqlDbType.VarChar, 500))

       cmdInsert.Parameters("@fname").Value = fname.Text

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Monday, July 20, 2009 11:54 PM by NYFP

This is exactly what I'm looking for too. But I'm using VB. Can you please help me out by converting the codes to VB. I'll be very grateful for that. Thanks!

Vishal

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Thursday, August 27, 2009 12:27 PM by guru_sarkar

NYFP,

Sorry for getting back too late...but still

Check this C# to VB.NET Converter:

www.developerfusion.com/.../csharp-to-vb

# re: Storing User Profile into a Custom Table using CreateUser Wizard control

Saturday, August 29, 2009 8:15 PM by KevinNg

This code doesn't work for me. It is not been executed.

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)

       {

           MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);

           Guid newUserId = (Guid)newUser.ProviderUserKey;

           //Get Profile Data Entered by user in CUW control

           String FirstName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtFName")).Text;

           String LastName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtLName")).Text;

Then i put Response.Write("Executed?"); and a break point. It doesn't show nor break at that point.

Mind telling me why?

It would be best that u can email to me at nkm.kevin@live.com

Thank you...

Leave a Comment

(required) 
(required) 
(optional)
(required)