Forum Moderators: open
Imports System.Web.Security
Partial Public Class AddUser
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
'if they click the add button
Private Sub addUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addUser.Click
'all of the input from the user
Dim username As String = userNameInput.Text
Dim password As String = passwordInput.Text
Dim password2 As String = passwordInput2.Text
Dim email As String = emailInput.Text
Dim question As String = questionInput.Text
Dim answer As String = answerInput.Text
Dim success As Boolean = True
'check to make sure they entered a valid email
If (Utilities.IsValidEmail(email)) Then
'check to make sure the passwords match
If (password = password2) Then
'the user object
Dim newUser As MembershipUser
'try to create the new user
Dim result As MembershipCreateStatus
Try
newUser = Membership.CreateUser(username, password, email, question, answer, True, result)
Catch ex As MembershipCreateUserException
output.Text = GetErrorMessage(ex.StatusCode)
success = False
Catch ex As HttpException
output.Text = ex.Message
success = False
End Try
'if this is true then the memeber was created
'as of now we are only using one role
'so we just assing the role of super admin.
If (success) Then
Response.Write(newUser.UserName)
End If 'if success
Else 'the passwords didn't match
output.Text = "<span class='textRed'>Your passwords did not match.</span>"
End If 'if password = password2
Else 'the email was invalid
output.Text = "<span class='textRed'>Please enter a valid email.</span>"
End If 'if isvalid email
End Sub 'add user click
'this function returns the proper error message if the create user fails
Private Function GetErrorMessage(ByVal status As MembershipCreateStatus) As String
Select Case status
Case MembershipCreateStatus.DuplicateUserName
Return "Username already exists. Please enter a different user name."
Case MembershipCreateStatus.DuplicateEmail
Return "A username for that e-mail address already exists. Please enter a different e-mail address."
Case MembershipCreateStatus.InvalidPassword
Return "The password provided is invalid. Please enter a valid password value."
Case MembershipCreateStatus.InvalidEmail
Return "The e-mail address provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidAnswer
Return "The password retrieval answer provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidQuestion
Return "The password retrieval question provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.InvalidUserName
Return "The user name provided is invalid. Please check the value and try again."
Case MembershipCreateStatus.ProviderError
Return "The authentication provider Returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."
Case MembershipCreateStatus.UserRejected
Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."
Case Else
Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
End Select
End Function
End Class
I have noticed that when I uncomment the response.write line I get a null reference error meaning that newUser is nothing. I have no idea what I am doing wrong. Any suggestions? Thanks,
You should be using this result instead of a try/catch block (see my earlier post [webmasterworld.com])
If (success) Then
If (result <> MembershipCreateStatus.Success) then
output.Text = GetErrorMessage(result)
ELSE
Response.Write(newUser.UserName)
END IF
End If 'if success
Dim result As MembershipCreateStatus
newUser = Membership.CreateUser(username, password, email, question, answer, True, result)Select Case result
Case MembershipCreateStatus.DuplicateEmail
' Dupe Email
Case MembershipCreateStatus.DuplicateUserName
' Dupe User Name
... etc. etc.
End Select