Question

I have the following code which creates a memebrship user, then calls other methods to insert the users other info into my SQL DB. The user is getting created. The info is getting into my database. Everything is working like it should, but for some reason, Rather then sending a success message to my Literal0.text, it always says "There is already a user with this email address.", even after the user is successfully created.I even added a check for Session("Created") to keep the code from running twice, still no luck. It works fine, just gives the wrong message at the end.

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Not Session("Created") = "True" Then
        Literal0.Text = ""
        Dim FName As String = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
        Dim LName As String = LNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
        Username = FName + "." + LName
        Dim SecretQ As String = "Please use the Contact Us page to request a reset password"
        Dim SecretA As String = "ergwergkqejfqeoufwqeofiheowfqpkoadmvnwo"
        Dim psw As Int32 = (TimeOfDay.Hour * TimeOfDay.Minute) * 863490
        Dim firstsix As String = psw.ToString().Substring(0, 6)
        password = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "") + firstsix + "!"
        Dim createStatus As MembershipCreateStatus
        Dim newUser As MembershipUser = _
        Membership.CreateUser(Username, password, _
        Email.Text, SecretQ, _
        SecretA, True, _
        createStatus)
        Select Case createStatus
            Case MembershipCreateStatus.Success
                Session("Created") = "True"
                If Not String.IsNullOrEmpty(BlogSettings.Instance.SelfRegistrationInitialRole) Then
                    Dim role As String = Roles.GetAllRoles().FirstOrDefault(Function(r) r.Equals(BlogSettings.Instance.SelfRegistrationInitialRole, StringComparison.OrdinalIgnoreCase))
                    If Not String.IsNullOrEmpty(role) Then
                        Roles.AddUsersToRoles(New String() {Username}, New String() {role})
                    End If
                End If
                Dim pf = AuthorProfile.GetProfile(Username)
                pf = New AuthorProfile(Username)
                pf.DisplayName = Username
                pf.EmailAddress = Email.Text
                pf.FirstName = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
                pf.LastName = LNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
                pf.[Private] = True
                pf.Save()
                Literal0.Text = ""
                AddUser()
                Exit Select
            Case MembershipCreateStatus.DuplicateUserName
                Literal0.Text = "There is already a user with this username."
                Exit Select
            Case MembershipCreateStatus.DuplicateEmail
                Literal0.Text = "There is already a user with this email address."
                Exit Select
            Case MembershipCreateStatus.InvalidEmail
                Literal0.Text = "There email address you provided in invalid."
                Exit Select
            Case MembershipCreateStatus.InvalidAnswer
                Literal0.Text = "There security answer was invalid."
                Exit Select
            Case MembershipCreateStatus.InvalidPassword
                Literal0.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character."
                Exit Select
            Case Else
                Literal0.Text = "There was an unknown error; the user account was NOT created."
                Exit Select
        End Select
    End If
End Sub
Was it helpful?

Solution

I would suggest to check the code from the parts below:

please make some break points to the application, we should check if every time the debug point will go to the case below:

Case MembershipCreateStatus.DuplicateEmail
                Literal0.Text = "There is already a user with this email address."
                Exit Select

and please make sure the email do not exist in the DataTable.

For more information about the MembershipCreateStatus, please refer to the link below:

http://msdn.microsoft.com/en-us/library/system.web.security.membershipcreatestatus.aspx

Hope it's useful for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top