Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello,

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.


Thanks
Posted

1 solution

Add following code in your webconfig file

XML
<authentication mode="Forms">
            <forms loginUrl="default.aspx">
            </forms>
        </authentication>
        <membership defaultProvider="defaultMembershipProvider">
            <providers>
                <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"/>
            </providers>
        </membership>
        <roleManager enabled="true">
            <providers>
                <clear/>
                <add name="AspNetSqlRoleProvider" connectionStringName="ConnectionStringNmae" applicationName="/TESTProject" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            </providers>
        </roleManager>


Then

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.


C#
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  + "@test.com", "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() );

                          try
                          {


                              txtName.Text = "";
                              txtPassword.Text = "";

                              ddUserGroup.SelectedValue = "0";
                              lblError.Text = "";
                              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('User created successfully!');</script>", false);


                          }
                          catch
                          {
                              Membership.DeleteUser(txtUserName.Text  );

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


                          }



                      }
                      else
                      {
                          lblError.Text = "Invalid Type Name.";
                      }
                      break;

                  case MembershipCreateStatus.DuplicateUserName:
                      lblError.Text = "";
                      lblError.Text = "Username already exists. Please enter a different user name.";
                      break;
                  case MembershipCreateStatus.DuplicateEmail:
                      lblError.Text = "";
                      lblError.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
                      break;
                  case MembershipCreateStatus.InvalidPassword:
                      lblError.Text = "";
                      lblError.Text = "The password provided is invalid. Please enter a valid password value. The Password Length Min 7 character";
                      break;
                  case MembershipCreateStatus.InvalidEmail:
                      lblError.Text = "";
                      lblError.Text = "The e-mail address provided is invalid. Please check the value and try again.";
                      break;
                  case MembershipCreateStatus.InvalidUserName:
                      lblError.Text = "";
                      lblError.Text = "The user name provided is invalid. Please check the value and try again.";
                      break;
                  default:
                      lblError.Text = "";
                      lblError.Text = "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                      break;

              }

          }
 
Share this answer
 
v2
Comments
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=2.0.0.0, 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.

type="System.Web.Security.SqlMembershipProvider"

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900