tracking sessions

  • Thread starter Thread starter mike parr
  • Start date Start date
M

mike parr

I need to keep track of users details by session variables and I have
been doing this by putting this code in the default page :

public void Page_Load()
{
if (!(Page.IsPostBack))
{
Session["LiveSession"] = "True";

string strSession;
strSession = "INSERT INTO Session(Time_Stamp) VALUES(GETDATE())
SELECT @@IDENTITY AS 'Identity'";

SqlConnection objConnectionSession = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectServerMcCallu
m"]);
SqlCommand objCommandSession = new SqlCommand(strSession,
objConnectionSession);

// open the connection to the database
objConnectionSession.Open();

// execute the SQL statement, returns 1 row only
Session["SessionID"] = objCommandSession.ExecuteScalar().ToString();

objConnectionSession.Close();
}

So I have a Session ID to identify the user when I am later writing to
my database, and I check if Session["LiveSession"] = true when I load a
new page to check if the session has timed out.

Do I need to prevent the user from clicking Back to this page where I am
setting all this session stuff up, or will this code only ever be
reached on the first load of the page?


Thanks,

Mike
 
you can rather use Session_Start method in Global.asax file. have this code
over there.

Av.
 
Doesnt' matter ? You'll just set the value agzain and refresh the timestamp
info.

As a side note, note that the Session object provides a property that allows
to now if the session is newly created. could it be suitable for your needs
?

Patrice
 
Back
Top