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#:

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

}

}

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:

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.

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

Comments

# 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 1, 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 1, 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 7, 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 7, 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 7, 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 7, 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 8, 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 8, 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...

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

Tuesday, December 8, 2009 5:46 PM by lookingforanswers

Currently our website uses forms authentication with a custom profile for each user. Right now, each user is associated with one company. However, I want to update our website so that one user could access 4 different companies while another user could access 3 different ones. Roles will not work for my situation. I was hoping to still use the membership and profile classes that I have. What I really want to do is each user has a user record in the user table (for membership), but they could have multiple rows in the profile table...and then load the correct profile into the Context.Profile when they change what company they are associated with. Is there any clean way to do this with asp.net forms that I'm not seeing?

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

Tuesday, December 15, 2009 9:16 AM by csharpcoder

Re: to lookingforanswers

I'm actually doing the same exact thing and what I did was create the custom table as described above added a column for the companies, due to my admins can be in charge of 4 companies at a time, I added 4 company columns. and on the create user wizard I added a checkbox if more then one company should be added to the user being created. I got it working perfectly

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

Friday, January 8, 2010 8:01 AM by abdo

how to extract the value of the default createUSerWizard fields????? please helppppppppppppppppppppppp

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

Friday, January 8, 2010 10:55 AM by guru_sarkar

abdo,

If your CUW ID is CreateUserWizard1 then you can get the username by:

CreateUserWizard1.UserName.

You can also find the UserName Textbox control as mentioned in the article similar to finding FirstName,LastName TBxs.

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

Saturday, January 23, 2010 4:45 AM by Virat Shukla

In membership table it stores data but in User_Profile nothing

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

Saturday, January 23, 2010 12:23 PM by guru_sarkar

Virat,

are you getting any errors? did you set breakpoints and see if your code is executed?

I doubt your code is executed.

Can you post your code and probelm here: http://forums.asp.net/25.aspx

Then post your thread's link here and I will look into it.

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

Tuesday, February 16, 2010 1:51 AM by Ravi Gupta

Thank You for a nice peice of article. I was looking for this for a great deal of time.

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

Thursday, February 25, 2010 1:30 PM by Jeetal

Thanks a lot! This was exactly what I was looking for :)

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

Tuesday, April 13, 2010 8:22 AM by Ashwini

Can we use update query instead of insert query directly in  CreateUserWizard1_CreatedUser function?

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

Tuesday, April 13, 2010 12:06 PM by guru_sarkar

Ashwini,

yes..you can do whatever coding you need in CreateUser handler. Now when you say update query I assume you have data in your table to update...and a mapping UserId which actually is a bit complex so it may or may not be doable....

I need some more information on what you are trying to achieve.

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

Thursday, September 23, 2010 9:50 AM by king

Thanks great stuff. this is what am looking for.

PLEASE can you write another tutorial on how to enable user edit profile information?

THANKS

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

Sunday, October 31, 2010 5:31 PM by XayBlu

Why is it that i do not have the type membership user? I have the aspnetdb and i use the user_ID as foreign key in one of my tables. I am trying to insert the user_ID when user is created from the CUW. I added extra fields but i can't get the user_ID from the aspnet_user table?

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

Sunday, October 31, 2010 5:57 PM by XayBlu

I have placed a few textboxes in the CUW for some input to another table however when i go to the button click action and try to insert the text to the table the text boxes do not appear to be accessible?

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

Monday, November 1, 2010 1:14 PM by guru_sarkar

XayBlu,

1: You will need reference to System.Web.Security namespace to get MembershipUser.

2: You will have to do FindControl as shown in the code. To get better idea of your issue, can you post your question here - http://forums.asp.net/25.aspx - with more details and post the link to that thread here. I will take a look at it.

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

Tuesday, January 25, 2011 10:05 AM by Aldrin Butcon

I've done the instructions and codes given above, there are no build errors, but when i clicked the "create user" button, an error "Object reference not set to an instance of an object" occurred.

What do you think is the problem?

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

Tuesday, January 25, 2011 1:52 PM by guru_sarkar

@Aldrin Butcon:

Which line is throwing exception?

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

Wednesday, February 2, 2011 1:50 AM by Aldrin Butcon

Sorry for the late reply..

The problem is in this line:

myCommand.ExecuteNonQuery()

I think the problem is in my ConnetionString, but I tried to add a SqlDataSource to my page then renamed MembershipConnectionString to ConnectionString. Well, it worked for me, but is it necessary to have a SqlDataSource.? Thanks in advance...

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

Wednesday, February 2, 2011 5:44 PM by guru_sarkar

Aldrin,

Not it is not necessary to have SQL Datasource on the page. The code assumes you have a connectionstring name "MembershipConnectionString" in your web.config. So it seems it wasn't able to find one and since you changed the name to "ConnectionString" which I believe was added to your web.config when you added sqlDataSource.

So you don't need a sqldatasource but you surely need a valid connectionstring in your web.config.

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

Thursday, February 3, 2011 3:01 AM by Aldrin Butcon

Now I understand, thanks there genius...

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

Monday, March 7, 2011 11:43 PM by guru_sarkar

firby,

Are you getting any errors? Did you place breakpoints and see if you save code is executed?

Can you post your question here - http://forums.asp.net/25.aspx - with more details and post the link to that thread here. I will take a look at it.

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

Thursday, March 17, 2011 2:04 AM by jiun

hi

the code works but why am i getting 2 same records inserted?

any idea?

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

Sunday, March 20, 2011 12:10 PM by guru_sarkar

jiun,

Looks like your CreatedUser event is fired twice. If you are using vb.net and have hooked up your event in your .aspx code then remove "Handles CreateUserWizard1.CreatedUser" from the code-behind. Can your share your CUW markup?

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

Monday, April 4, 2011 2:23 PM by Gus

On This line "Dim newUser As Membership = Membership.GetUser(CreateUserWizard1.UserName)

I received the following:

Value of type System.Web.Security.MembershipUser cannot be converted to System.Web.Security.Membership

email address: gvt99@bellsouth.net

I apreciate any help

Thank you

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

Monday, April 4, 2011 8:05 PM by guru_sarkar

@Gus,

My bad, the code-converter did something wrong. Check I updated that code line. It should be:

Dim newUser As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)

Thanks for pointing out.

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

Monday, April 4, 2011 10:46 PM by Gus

Works. Thank you

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

Monday, April 18, 2011 1:52 AM by weblogs.asp.net

Storing user profile into a custom table using createuser wizard control.. Ho-o-o-o-t :)

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

Thursday, May 5, 2011 4:38 AM by weblogs.asp.net

Storing user profile into a custom table using createuser wizard control.. Smashing :)

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

Friday, May 6, 2011 2:56 AM by Abid Ali

Thanks dear it is working for me

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

Tuesday, June 28, 2011 2:08 PM by guru_sarkar

Hardik,

Sorry for late reply. The only reason I can think of is it is not able to find/access your Sql Server. May be you want to check your connection string is correctly pointing to right server/database and the account has proper rights.

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

Tuesday, November 15, 2011 5:05 PM by Anahita

Great Tip

Thanks alot

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

Friday, March 2, 2012 7:29 PM by Kangla

This is a nice article.

"What if something goes wrong between UserCreation and storing profile" To address this should the below works

try

{

----- Whatever the above code here.

}

catch (Exception RegisterException)

       {

           Trace.Write(RegisterException.ToString());

           Membership.DeleteUser(((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text);

       }

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

Monday, March 5, 2012 11:17 AM by guru_sarkar

Thanks Kangla for the inputs. Good point.

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

Tuesday, March 20, 2012 6:54 AM by cheiftain

I'm new to programming, I cant see any connection string in my web.config file can I add it and if so how?

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

Monday, March 26, 2012 1:04 PM by guru_sarkar

chieftain,

Sorry for late reply. If you are using default membership provider, the default connection string -"LocalSqlServer" is taken from machine.config. You can override this by adding new connection to your web.config and adjusting the membership provider connectionStringName property.

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

Thursday, May 10, 2012 1:45 AM by mahfuz01

Its a fantastic post (almost the one I was looking for). I want to store the information into my own table [tblUserInfo] in my own Database [dbProject] and do not want to add data into the aspnet_Users table but use the Membership functionalities to store profile info. Any lead? Thanks in advance.

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

Wednesday, August 1, 2012 2:55 AM by Lasantha

really nice explanation bro.... good job!

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

Tuesday, January 29, 2013 2:02 PM by Joe F

This is a great tutorial, thanks.  One tip, add:

ON DELETE CASCADE

...at the end of your CONSTRAINT in your User_Profile table.  This will allow user deletes from the primary table to cascade to this table also.

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

Wednesday, April 24, 2013 11:09 AM by hozdaman

Hi,

This is a great tutorial!  Thanks.

How can I add a check to see if if the aspnet_Users table values have been inserted, before the fields for my other table get inserted.

Im finding that foreign key is trying to insert before the aspnet_Users table is updated. Causing a foreign key constraint.

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

Tuesday, May 7, 2013 12:34 PM by guru_sarkar

@hozdaman,

CreatedUser event is fired only after user is created. And the code is retrieving the ProviderUserKey. So not sure if you are using some different event or something else would be an issue.

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

Thursday, June 20, 2013 5:43 PM by faujong

Hi,

I posted the question on forums.asp.net/.../1.

Can you help ?

Thank you.

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

Sunday, July 28, 2013 1:14 AM by David

Hello Guys,

Everything was fine except the below error that I am getting once I click the "Create User" button. Please also note that the data entered into original fields are correctly inserted into the database but it does not gets inserted from the additional fields that I have created and it throws the below error:

Server Error in '/' Application.

Keyword not supported: 'metadata'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Keyword not supported: 'metadata'.

Source Error:

Line 41:

Line 42:

Line 43:         using (SqlConnection myConnection = new SqlConnection(connectionString))

Line 44:

Line 45:         {

CAN SOMEONE PLEASE LET ME KNOW WHAT IS THE CAUSE FOR THESE?

Regards,

David

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

Monday, July 29, 2013 7:56 PM by guru_sarkar

David,

It should be something in your page code. Check for "metadata" and post the relevant code.

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

Monday, August 26, 2013 2:35 PM by Nita

I am trying to add First and Last name of the user to the wizard. It inserts all the fields except for custom fields in the "aspnet_UserProfile" table which I created. I get this error for "myCommand.ExecuteNonQuery()" Invalid object name 'aspnet_UserProfile'. I am using vbs2012.

<asp:CreateUserWizard runat="server" id="CreateUserWizard1" CssClass="cctr" DisplayCancelButton="True" PasswordLabelText="*Password:" QuestionLabelText="**Security Question:" OnCreatedUser="CreateUserWizard1_CreatedUser">

I have pasted your code for the event. My insert SQL is correct as I did run the query for the table and it did insert.

Thank you for all your documentation.

~ Nita

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

Monday, August 26, 2013 2:47 PM by guru_sarkar

Nita,

'aspnet_UserProfile' is your new table correct? And you are trying to save using inline query or stored procedure?

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

Monday, August 26, 2013 3:32 PM by Nita

I am ttying to use the inline query; Yes aspnet_UserProfile is my new table. This is what I have:

Protected Sub CreateUserWizard1_CreatedUser(sender As Object, 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("txtFName"), TextBox).Text

       Dim LastName As String = DirectCast(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtLName"), TextBox).Text

       ' Insert a new record into User_Profile

       ' Get your Connection String from the web.config.

       Dim connectionString As String = ConfigurationManager.ConnectionStrings("IDBConnectionString").ConnectionString

       Dim insertSql As String = "INSERT INTO aspnet_UserProfile(UserId, FirstName, LastName) VALUES(@UserId, @FirstName, @LastName)"

       Using myConnection As New Data.SqlClient.SqlConnection(connectionString)

           myConnection.Open()

           Dim myCommand As New Data.SqlClient.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

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

Monday, August 26, 2013 3:50 PM by guru_sarkar

Nita,

Is your new table in the same database as your membership tables or different?

To what is the database your IDBConnectionString pointing at?

As per the error aspnet_UserProfile is not found.

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

Monday, August 26, 2013 3:59 PM by Nita

IDB is the database for the system. ASPNETDB.MDF is the aspnet membership database. They are two different database, ok so I need the connection string for ndf file right?

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

Monday, August 26, 2013 4:17 PM by guru_sarkar

Yes, you will need the connectionstring to where your aspnet_UserProfile table is.

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

Tuesday, August 27, 2013 12:45 AM by Nita

Thanks Mr.Guru. I am going to transfer the database to the server and there I will merge them together. Currently I am working on my desktop.

~ Nita

Leave a Comment

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