p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Return UserId from aspnet_Users (http://p2p.wrox.com/showthread.php?t=48651)

rao965 October 4th, 2006 05:32 PM

Return UserId from aspnet_Users

I am tring to grab UserId from aspnet_users table in aspnet.db after user X loggen in and put it in a nother table as forien key in my custom databse.

could anyone let me know how i can do that

many thanks

timeware October 5th, 2006 03:41 AM

Once the user has logged in then you can find out User.Identity.Name

You can then use this to return the UserId and therefore reference to aspnet_Membership, aspnet_sersInRoles etc.

If anyone knows a more elegant way then please post it here as I would also be interested

hcusto October 5th, 2006 04:04 AM

First include "System.Web.Security" namespace


using System.Web.Security;
then in your method (function) where you want to get user id write:


if you want to add it to the string you must convert it to string but first check if the user id logged in

string userID;

if (User.Identity.IsAuthenticated)
      userID = Membership.GetUser().ProviderUserKey.ToString();

Hope that this helps,

surekha March 15th, 2007 12:17 AM

    I am also trying to get userId. I am using the following command:
I am assigning this value to a guid variable. But, I am getting a error like:

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."

Hope someone will help me.


Tremmorkeep July 12th, 2007 04:50 PM

checking to see if the user is authenticated is critical at this point, my guess is that you dont have a user auth. so you're trying to get a providerkey for a null user. GetUser() also accepts a string argument so you can specify which user you want (ie GetUser("Steve") if you are editing data for users on your site.

Hope that helps.

All times are GMT -4. The time now is 08:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.