Link to home
Create AccountLog in
Avatar of robaherne
robaherne

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of elimesika
elimesika
Flag of Israel image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of robaherne
robaherne

ASKER

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
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.
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