1

How to add user to roles during CreateUserWizard.CreatedUser?

What's wrong in this code:

Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser
    Dim username As TextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("UserName")
    Dim literal1 As Literal = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Literal1")
    Dim sql As SqlDataSource = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("SqlDataSource1")
    Dim user As MembershipUser = Membership.GetUser(username.Text)
    Dim pwd As TextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Password")
    Dim obj As Object
    obj = user.ProviderUserKey
    sql.InsertParameters.Add("UserId", obj.ToString)
    Roles.AddUserToRole(CreateUserWizard1.UserName, literal1.Text.ToString)
    sql.Insert()
End Sub

Error:

The parameter 'username' must not be empty. Parameter name: username.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user583181
  • 75
  • 1
  • 5
  • 21
  • Do you have any/default Membership provider installed? Are you using built-in membership/role infrastructure/database? – abatishchev Jan 22 '11 at 18:21

1 Answers1

0

I am not familiar with the wizard you are using, are you getting the error on the line where you dim user? or later on the roles.addUserToRole?

If you are only getting it on the roles.addUserToRole, why not change CreateWizard1.UserName to username.text as you used when you dimmed user?

Jay
  • 5,897
  • 1
  • 25
  • 28