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.
.NET ProgrammingEditors IDEsMicrosoft SQL Server 2005

Avatar of undefined
Last Comment
robaherne

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
elimesika

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
robaherne

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

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23