Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Adding user to role

Avatar of Peter Nordberg
Peter NordbergFlag for Sweden asked on
ASP.NET
1 Comment1 Solution314 ViewsLast Modified:
I'm using asp.net 2.0 create user wizard. When I'm adding a user to a role automatically when registering them I get an error that I can't figure out how to come around. Here's the code:

.aspx page
----------------------------------------------------------------------------------------------------------------------------------------------
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" CreateUserButtonStyle-CssClass="button" OnCreatedUser="CreateUserWizard1_CreatedUser">
         <WizardSteps>
                 <asp:WizardStep ID="PersonalInformation" runat="server" Title="Personlig information" StepType="Start">
                <table style="width: 108px">
                    <tr>
                        <td style="width:25%">
                        <asp:Label ID="lblFirstName" Text="F&#246;rnamn" runat="server"></asp:Label>
                        </td>
                        <td style="width: 75%">
                            <asp:TextBox ID="txtFirstName" runat="server" CssClass="text"></asp:TextBox>
                        </td>
                    </tr>
                     <tr>
                        <td style="width:25%">
                        <asp:Label ID="lblLastName" Text="Efternamn" runat="server"></asp:Label>
                        </td>
                        <td style="width: 75%">
                            <asp:TextBox ID="txtLastName" runat="server" CssClass="text"></asp:TextBox>
                        </td>
                    </tr>    
                </table>
            </asp:WizardStep>
            <asp:CreateUserWizardStep runat="server">
                <ContentTemplate>
                    <table border="0">
                        <tr>
                            <td align="center" colspan="2" style="height: 15px">
                                <strong>
                                Skapa nytt användarkonto</strong></td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Användarnamn:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="UserName" runat="server" CssClass="text"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                    ErrorMessage="Du måste ange ett användarnamn." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Lösenord:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" TextMode="Password" CssClass="text"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                    ErrorMessage="Du måste välja ett lösenord." ToolTip="Du måste välja ett lösenord." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Bekräfta lösenord:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password" CssClass="text"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                                    ErrorMessage="Du måste bekräfta lösenordet." ToolTip="Du måste bekräfta lösenordet."
                                    ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-post:</asp:Label></td>
                            <td>
                                <asp:TextBox ID="Email" runat="server" CssClass="text"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
                                    ErrorMessage="Du måste ange en e-post adress." ToolTip="Du måste ange en e-post adress." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                    ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="Lösenorden stämmer inte överens."
                                    ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2" style="color: red">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:CreateUserWizardStep>
           
       
            <asp:CompleteWizardStep runat="server">
            </asp:CompleteWizardStep>
        </WizardSteps>
        <CreateUserButtonStyle CssClass="button" />
        <StartNavigationTemplate>
            <asp:Button ID="StartNextButton" runat="server" CommandName="MoveNext" Text="Next" CssClass="button" />
        </StartNavigationTemplate>
    </asp:CreateUserWizard>

-------------------------------------------------------------------------------------------------------------------------------------------

vb code
--------------------------------------------------------------------------------------------------------

 Profile.FirstName = txtFirstName.Text
        Profile.LastName = txtLastName.Text
        Roles.AddUserToRole(CreateUserWizard1.UserName, "Editor")
---------------------------------------------------------------------------------------------

Error message:
----------------------------------------------------------------------------------------------

The user 'jonasK' is already in role 'Editor'.
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 user 'jonasK' is already in role 'Editor'.

Source Error:


Line 10:         Profile.FirstName = txtFirstName.Text
Line 11:         Profile.LastName = txtLastName.Text
Line 12:         Roles.AddUserToRole(CreateUserWizard1.UserName, "Editor")
Line 13:     End Sub
Line 14: End Class
 

Source File: D:\WebSiter\SkipperLife\admin\Users\add_user.aspx.vb    Line: 12

Stack Trace:


[ProviderException: The user 'jonasK' is already in role 'Editor'.]
   System.Web.Security.SqlRoleProvider.AddUsersToRolesCore(SqlConnection conn, String usernames, String roleNames) +2192082
   System.Web.Security.SqlRoleProvider.AddUsersToRoles(String[] usernames, String[] roleNames) +705
   System.Web.Security.Roles.AddUserToRole(String username, String roleName) +341
   admin_add_user.CreateUserWizard1_CreatedUser(Object sender, EventArgs e) in D:\WebSiter\SkipperLife\admin\Users\add_user.aspx.vb:12
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.WebControls.CreateUserWizard.OnCreatedUser(EventArgs e) +105
   System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +341
   System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +105
   System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +453
   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

-----------------------------------------------------------------------------------

Thanks for help!
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers