views:

100

answers:

2

i have the following code:

        if (Membership.FindUsersByName(username) == null)
        {
            Membership.CreateUser(username, password, email);
        }

        if (!Roles.RoleExists("USR"))
        {
            Roles.CreateRole("USR");
        }
        Roles.AddUserToRole(username,"USR");

Data is being inserted in the aspnet_Users and aspnet_UsersInRoles, but data is not being inserted in the aspnet_membership and i need this data as i'm using the asp login control.

can someone help me out?

A: 

Try this out. I believe you are not passing all parameters.

string userName = txtUserId.Text;

//this value is either encrypted or hashed and is never displayed 
string password = txtPassword.Text; 

string email = txtEmail.Text;
string passwordQuestion = ddlPasswordQuestion.SelectedValue;

//this value is either encrypted or hashed and is never displayed 
string passwordAnswer = txtPasswordAnswer.Text;

MembershipCreateStatus result;
Membership.CreateUser(userName, password, email, passwordQuestion, passwordAnswer, true,out result);

lblResults.Visible = true;
switch (result) {
  case MembershipCreateStatus.Success:
    txtUserId.Text = null;
    txtPassword.Text = null;
    txtEmail.Text = null;
    ddlPasswordQuestion.SelectedIndex = -1;
    txtPasswordAnswer.Text = null;
    lblResults.Text = "User successfully created!";
    break;
  case MembershipCreateStatus.InvalidUserName:
    lblResults.Text = "The username format was invalid.  Please enter a different username.";
    break;
  case MembershipCreateStatus.InvalidPassword:
    lblResults.Text = "The password was invalid:  A password cannot be an empty string and must also meet the pasword strength requirements of the configured provider.  Please enter a new password.";
    break;
  case MembershipCreateStatus.InvalidEmail:
    lblResults.Text = "The email format was invalid.  Please enter a different username.";
    break;
  case MembershipCreateStatus.InvalidQuestion:
    lblResults.Text = "The password question format was invalid.  Please enter a different question.";
    break;
  case MembershipCreateStatus.InvalidAnswer:
    lblResults.Text = "The password answer format was invalid.  Please enter a different answer.";
    break;
  case MembershipCreateStatus.DuplicateUserName:
    lblResults.Text = "The username is already in use.  Please enter a new username.";
    break;
  case MembershipCreateStatus.DuplicateEmail:
    lblResults.Text = "The email address is already in use.  Please enter a different email address.";
    break;
  default:
    lblResults.Text = "An error occurred while creating the user.";
    break;
}

}

CodeToGlory
still the same problem. and the membership.createuser has an override with 3 paramters just like my example.
A: 

The problem is with the if statement. The reason why it doesn't work is because the FindUsersByName method returns a collection of MembershipUsers. There might not be any users, but a collection still gets returned.

Please change the statement to the following...

if (Membership.FindUsersByName(username).Count == 0)
{
    Membership.CreateUser(username, password, email);
}

That should do it.

jinsungy
thanks works perfectly now