views:

83

answers:

4
+4  Q: 

Session or Query

Should I store the user's ID, name & email address in a session variable or should I query for the user's user's ID, name & email address everytime i need to use it?

+1  A: 

Keep it in a session variable. Its better to cache your frequently used data in the application instead of having a bunch of round-trips to the server.

Lerxst
How was this worth a vote-down?
Lerxst
A: 

if you want to only store UserID, Name and Email, then session is more preferable rather than getting every time from DB. storing light weight object in session is not a big problem. but it can be problem when you store heavy object like datatable in session

Muhammad Akhtar
+1  A: 
public class UserInfo
{
   public int UserID {get;set;}
   public string Email {get;set;}
   ...
}

When a user logs on, create an instance of UserInfo and store it in the session.

Danny Chen
This is completely unnecessary if the user is using ASP.Net Membership providers. If you do this, you will be storing the same information twice for each user. From the MSDN docs: "When you call the ValidateUser method to check a user's credentials, the membership system does all the database lookup for you." Including getting their user information.
Daniel Dyson
+1  A: 

If you are using ASP.NET security, i.e. membership providers and role providers etc, then you don't have to store it. It is provided by the membership provider:

 MembershipUser myObject = Membership.GetUser();
 string UserID = myObject.ProviderUserKey.ToString();

Furthermore, if you are authenticating the user on each page, which happens automatically on a secure website, then the Membership provider doesn't have to go back to the database to get the data because it is already in scope for the current request.

If you are using the out-of-the-box profile provider, I would encourage you to use and download the table based provider mentioned in Scott Gu's blog: http://weblogs.asp.net/scottgu/archive/2006/01/10/435038.aspx It is a lot more friendly and performs a lot better than the default provider.

Daniel Dyson