Role not found (error)

Last post 01-08-2007 1:08 AM by Leijun Jie - MSFT. 2 replies.

Sort Posts:

  • Role not found (error)

    01-05-2007, 10:19 AM
    • Member
      1 point Member
    • hrmalik
    • Member since 01-05-2007, 3:07 PM
    • Posts 4

    Hi,

    I have created a new step in the CreateUserWizard control. The purpose of this step is to collect some information from the user, like his first name, last name, and most importantly his role. On clicking the Finish button for this step, I want to save the role in Roles and the other information in the user's profile. I have written the following code on the click event: 

    Protected Sub CreateUserWizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles CreateUserWizard1.FinishButtonClick 

            Roles.AddUserToRole(User.Identity.Name, UserTypes.SelectedItem.Value)

            Dim userProfile As ProfileCommon = CType(ProfileCommon.Create(CreateUserWizard1.UserName, True), ProfileCommon)
            With userProfile.UserInfo
                .UserTitle = UserTitle.SelectedItem.Value
                .FirstName = txtFName.Text
                .MiddleName = txtMName.Text
                .LastName = txtLName.Text
                .Role = txtRole.Text
                .Company = txtCompany.Text
            End With
            userProfile.Save()

    End Sub

    But, I receive the following error when this gets executed:

     

    The role '' was not found.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Configuration.Provider.ProviderException: The role '' was not found.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


    Stack Trace:

    [ProviderException: The role '' was not found.]
       System.Web.Security.SqlRoleProvider.AddUsersToRolesCore(SqlConnection conn, String usernames, String roleNames) +2189583
       System.Web.Security.SqlRoleProvider.AddUsersToRoles(String[] usernames, String[] roleNames) +705
       System.Web.Security.Roles.AddUserToRole(String username, String roleName) +341
       registerUser.CreateUserWizard1_FinishButtonClick(Object sender, WizardNavigationEventArgs e) +146
       System.Web.UI.WebControls.Wizard.OnFinishButtonClick(WizardNavigationEventArgs e) +105
       System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +693
       System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +149
       System.Web.UI.WebControls.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) +17
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
       System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +115
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    


    The problem lies in the line;

            Roles.AddUserToRole(User.Identity.Name, UserTypes.SelectedItem.Value)

    When I comment this line, everything works perfectly. Can anyone help me out with this? How can I have the roles also saved to the database?

  • Re: Role not found (error)

    01-05-2007, 1:21 PM
    • Member
      116 point Member
    • wildcats
    • Member since 04-11-2006, 8:51 PM
    • Posts 26
    You may be passing an empty string as the rolename and hence, the role cannot be found.
  • Re: Role not found (error)

    01-08-2007, 1:08 AM
    Answer
    Make sure UserTypes.SelectedItem.Value is something other than an empty string when you tried to add the user to role. Another thing is that you'd better use CreateUserWizard1.UserName instead of User.Identity.Name, that is:

    Roles.AddUserToRole( CreateUserWizard1.UserName, UserTypes.SelectedItem.Value)

    As I understand, the User.Identity will be null untill the page is post back.

    The future is now...
    Sincerely,
    LeiJun Jie
    Microsoft Online Community Support
Page 1 of 1 (3 items)