Search code examples
c#asp.netcreateuserwizard

Call method before creating user


When You press "Create user" in your ASP.NET standard CreateUserQizard the OnCreatedUser gets called and redirects you after the user is created. However I need to run some code before the user is created but after the "Create user" button is clicked.

Like this: Click "Create user" -> Run my method -> Create user -> OnCreatedUser

I have made my own method for checking if email already exists, however I don't know how to call it since the Wizard creates the user right away. Is there any way to get in before the user is created and execute my code?

My code:

TextBox EmailTextBox = RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email") as TextBox;
String emailParam = EmailTextBox.Text;

using (SqlConnection con = new SqlConnection(strCon))
    {
       con.Open();
       using (SqlCommand cmd = con.CreateCommand())
          {
            cmd.CommandText = "SELECT Email FROM Memberships WHERE Email=@emailParam";
            cmd.Parameters.AddWithValue("@emailParam", emailParam);

             using (SqlDataReader reader = cmd.ExecuteReader())
                {
                            if (reader.Read())
                            {
                                LabelExists.Text = "This e-mail is already registered";
                            }
                            else
                            {
                                //execute method that creates user
                            }

                }
             }
        con.Close();
   }

Solution

  • Simply use OnCreatingUser, and set e.Cancel = true; if e-mail is already registered.

    If you set requiresUniqueEmail="true" for the membership provider, it does this check for you.