Search code examples
asp.netmysqlguidcreateuserwizard

How to get the user ID from createuserwizard for mysql database?


I am using the createuserwizard to store additional user info into a mysql database, apart from the info provided by the asp.net membership provider. I would like to store this info into another table called 'userprofile'. The user id in 'userprofile' is of type int and is a foreign key referencing 'id' of 'my_aspnet_users.

This is my problem: I can't seem to get the user id of the newly created user to store in my 'userprofile' table. These lines don't work:

        MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
        Int32 newUserId = (Int32)newUser.ProviderUserKey;            

These lines are causing the username to be set to the guid value returned by ProviderUserKey, which is so odd! The id column from my_aspnet_users, on the other hand, gets incremented by 1 when the user is created. And nothing gets added to the 'userprofile' table.

This is where I inserted the above code:

       protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

        string continueUrl = RegisterUser.ContinueDestinationPageUrl;
        if (String.IsNullOrEmpty(continueUrl))
        {
            continueUrl = "~/";
        }

        // Retrieve all the values from the registration form to insert into database
        TextBox FirstNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("FirstName");
        TextBox LastNameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("LastName");
        TextBox UsernameTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("UserName");
        TextBox EmailTextBox = (TextBox)RegisterUserWizardStep.ContentTemplateContainer.FindControl("Email");

        // Get the UserId of the just-added user
        MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
        Int32 newUserId = (Int32)newUser.ProviderUserKey;            

        MySqlConnection DBConn = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString);
        MySqlCommand DBCmd = new MySqlCommand(); 

        try
        {
            // Add Insert statement to insert into database
            DBCmd = new MySqlCommand(
                "INSERT INTO userprofile(UID, Email, Fname, Lname)" +
                "VALUES (@UID, @Email, @Fname, @Lname)", DBConn);

            // Add database parameters
            DBCmd.Parameters.Add("@UID", MySqlDbType.Int32).Value = newUserId;
            DBCmd.Parameters.Add("@Email", MySqlDbType.VarChar).Value = EmailTextBox.Text;
            DBCmd.Parameters.Add("@Fname", MySqlDbType.VarChar).Value = FirstNameTextBox.Text;
            DBCmd.Parameters.Add("@Lname", MySqlDbType.VarChar).Value = LastNameTextBox.Text;
            DBCmd.ExecuteNonQuery();
        }

        catch(Exception exp){
            Response.Write(exp);
        }

        // Close database connection and dispose database objects
        DBCmd.Dispose();
        DBConn.Close();
        DBConn = null;

        Response.Redirect(continueUrl);

    }

Has anyone worked with mysql and the createuserwizard for additional info?

Any help would be appreciated. Thanks!


Solution

  • It turns out there is nothing wrong with the above code. I was messing around with the user id value in another function, which was causing trouble in here!