Alpha_Wing Alpha_Wing - 24 days ago 9
C# Question

.Net keep data on the object through out the application

I'm using visual studio 2013 and working on a asp web project. Since i'm new to the development, i have this issue.

The scenario is this. when a user login in, since this is a web service based project, apart from the username and passwords the service will return some values. so i have to keep those values such as user name, address, age throughout the application until he log out.

I have tried to keep the values using like this,

public class CONSTANTS
{
public static String address;
public static String age;
public static String tele;
}


so that i can use classname.address likewise. but when i do this, if two people try to access at the same time, it's not working and i know this is not a valid method. please help me on this. how should i implement this, it would be better if i have a small sample code. thank you.

Answer

You can implement this using session Variable as Session Remains different for all Users who Login in your Web Application.

You can store value in Session in your Login buttons click Event as.

 Session["username"]=xyz;
 Session["pass"]=*****;
 Session["age"]=20;

etc...

and you can also retrive values from session whenever Required as.

  string uname=Convert.Tostring(Session["username"]);
  string pass=Convert.Tostring(Session["pass"]);

etc..

make sure you need to explicitly cast value in your Desired type as Session return in Object form.