We help IT Professionals succeed at work.

How do I get the UserId from aspnet_Users after CreateUser() to use as a foreign key in another Table?

1,001 Views
Last Modified: 2013-11-07
I'm using 'CreateUser()' to insert a new user... but in the same event I want to retrieve the new created 'UserId' from 'aspnet_Users' and insert it into another table as a foreign key.

Many thanks.
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I've been following a tutorial on www.asp.net and the username doen't have to be unique. Is there any other way of doing it or do you think it'd best to make unique usernames a requirement?

Thanks

Author

Commented:
I'm quite happy to set it so that a new member requires a unique user name but how do I do that?

When catching a 'MembershipCreateUserException' one of the cases for an error message is as follows...

case MembershipCreateStatus.DuplicateUserName: return "There already exists a user with this username.";

This implies I can set the membership provider to only allow unique usernames. But all I can see in the Web.Config provider settings is...

requiresUniqueEmail="true"

but no requiresUniqueUserName option.

Author

Commented:
My mistake... somehow I have created to users with the same name but now it won't let me.

I can now user the Username in:

Membership.GetUser("username").ProviderUserKey.ToString();

And it works!

Thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.