Click here to Skip to main content
13,193,056 members (64,038 online)
Rate this:
Please Sign up or sign in to vote.

I have been searching and searching for a solution when it comes to programmatically creating users to the membership database, but nothing so far has worked for what I need.

I hope someone here can help me out or at least guide me in the right direction.

In a few sentences I will try to explain what I am trying to accomplish here. My first step was to create two membership databases in my SQL server to accommodate the separation of users when querying statistical data. "I thought it would be easier this way when pulling data from group A vs. group B"

As far as my second step I would like to programmatically create users using the CreateUserWizard control and be able to set which membership database to add users to.

To keep it short I am confident that I have the right code under my web.confg file to represent the two membership databases.

The steps should be easy like this right? 1. Call the control's event handler 2. Set connection to the membership database inside the event handler 3. Use the Membership.CreateUser method to add new users

However I get the blue squiggly line under the "Membership.CreateUser" method when adding users under the "CreateUserWizard1_CreatingUser" handler. "In VB.Net"

If I am not clear on what I am asking here, or you need more information, please, please be free to ask me.

Posted 27-Jun-12 8:06am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Add following code in your webconfig file

<authentication mode="Forms">
            <forms loginUrl="default.aspx">
        <membership defaultProvider="defaultMembershipProvider">
                <add name="defaultMembershipProvider" connectionStringName="ConnectionStringNmae" applicationName="ss" type="System.Web.Security.SqlMembershipProvider" requiresQuestionAndAnswer="true" passwordFormat="Clear" enablePasswordRetrieval="true" enablePasswordReset="true" minRequiredNonalphanumericCharacters="0" maxInvalidPasswordAttempts="999" passwordAttemptWindow="60" minRequiredPasswordLength="7"/>
        <roleManager enabled="true">
                <add name="AspNetSqlRoleProvider" connectionStringName="ConnectionStringNmae" applicationName="/TESTProject" type="System.Web.Security.SqlRoleProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>


execute your regsql.exe and define your db .The Membership table automatically create.

In your user creation page , add following code when submit your details.

  if (Page.IsValid)
                MembershipCreateStatus UserCreateStatus;
                //MembershipUser uu = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmailid.Text, "1", "1", true, out UserCreateStatus);
                MembershipUser uu = Membership.CreateUser(txtUserName.Text  , txtPassword.Text, txtUserName.Text  + "", "1", "1", true, out UserCreateStatus);
                switch (UserCreateStatus)
                    case MembershipCreateStatus.Success:
                        Object[] _Obj = new Object[] { 0, 1, 2, 3, 4 };
                        if (Roles.RoleExists(ddUserGroup.SelectedItem.ToString() + "_" + Session["CompanyID"].ToString()))
                            Roles.AddUserToRole(txtUserName.Text , ddUserGroup.SelectedItem.ToString() );
                                txtName.Text = "";
                                txtPassword.Text = "";
                                ddUserGroup.SelectedValue = "0";
                                lblError.Text = "";
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('User created successfully!');</script>", false);

                                Membership.DeleteUser(txtUserName.Text  );
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('User unable to create!');</script>", false);


                            lblError.Text = "Invalid Type Name.";
                    case MembershipCreateStatus.DuplicateUserName:
                        lblError.Text = "";
                        lblError.Text = "Username already exists. Please enter a different user name.";
                    case MembershipCreateStatus.DuplicateEmail:
                        lblError.Text = "";
                        lblError.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
                    case MembershipCreateStatus.InvalidPassword:
                        lblError.Text = "";
                        lblError.Text = "The password provided is invalid. Please enter a valid password value. The Password Length Min 7 character";
                    case MembershipCreateStatus.InvalidEmail:
                        lblError.Text = "";
                        lblError.Text = "The e-mail address provided is invalid. Please check the value and try again.";
                    case MembershipCreateStatus.InvalidUserName:
                        lblError.Text = "";
                        lblError.Text = "The user name provided is invalid. Please check the value and try again.";
                        lblError.Text = "";
                        lblError.Text = "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
Member 3491662 28-Jun-12 9:27am
Thanks Jeeva for the code.

As I was reviewing your web.config file sample, I notice the first default provider is set to type="System.Web.Security.SqlMembershipProvider". The second default provider is set to type="System.Web.Security.SqlRoleProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Now if I am trying to connect to two different membership databases, wouldn't the type property be the same as the first default provider.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171017.2 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100