Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 27-Jun-12 8:06am

1 solution

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

Solution 1

Add following code in your webconfig file
 
<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.
 

  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;
 
                }
               
            }
  Permalink  
v2
Comments
Member 3491662 at 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)

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 279
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2014
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