CreateUserWizard - Adding Fields problem

th3gman

Member
Joined
Nov 17, 2010
Messages
16
Programming Experience
Beginner
All,

For the life of me I can't figure out how to get the data from the two fields I have added and then insert it into my aspnet_Membership table.

I've added the two columns to the table.

Added this to my aspx page:
VB.NET:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:teal_data %>" InsertCommand="INSERT INTO [aspnet_Membership] (FirstName, LastName) VALUES (@FirstName, @LastName)" ProviderName="ConnectionStrings:teal_data.CustomizedProvider">
                               <InsertParameters>
                                <asp:Parameter Name="FirstName" Type="String" />
                                <asp:Parameter Name="LastName" Type="String" /> 
                               </InsertParameters>
                            </asp:SqlDataSource>

And this to my aspx.vb file:
VB.NET:
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs)
        Dim UserNameTextBox As TextBox = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"), TextBox)
        Dim DataSource As SqlDataSource = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("SqlDataSource2"), SqlDataSource)

        Dim User As MembershipUser = Membership.GetUser(UserNameTextBox.Text)
        Dim UserGUID As Object = User.ProviderUserKey

        DataSource.InsertParameters.Add("UserId", UserGUID.ToString())
        DataSource.Insert()
    End Sub

But the FirstName and LastName textbox info is never inserted.

Thanks for the help!
 
I'm basically banging my head up against the wall here... I've tried all sorts of things.

Can someone walk me through adding two fields (FirstName and LastName) to the CreateUserWizard and placing that data in the aspnet.Membership table (I've already created two new columns (FirstName and LastName).

Thank you!
 
Nevermind! Figured it out myself... Posting what worked in case someone runs into a similar issue.

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.
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("yourstringhere").ConnectionString

        Dim insertSql As String = "UPDATE aspnet_Membership SET FirstName = @FirstName, LastName = @LastName WHERE UserId = @UserId"
        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

And make sure this is in your asp page (specially the "oncreateuser" piece)
VB.NET:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" oncreateduser="CreateUserWizard1_CreatedUser">
 
Last edited:
Back
Top