By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

tracking sessions

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
you can rather use Session_Start method in Global.asax file. have this code
over there.

Av.
"mike parr" <mp********@yahoo.co.uk> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #2

P: n/a
The "back" button isn't a postback, so you need to prevent duplicates.

"mike parr" <mp********@yahoo.co.uk> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #3

P: n/a
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
"mike parr" <mp********@yahoo.co.uk> a écrit dans le message de
news:Ob**************@TK2MSFTNGP09.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.