473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Trouble with using Session state

I'm having difficulty understanding Session state in ASP.Net. It's almost
embarrassing asking this as I've been using ASP since it was first released
& it really shouldn't be this hard to use - perhaps I'm just not very smart
or perhaps MS is making this too hard for us sql bunnies to understand - I
dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax. (also
very aware of scalability issues - so don't flame me on this - I'm just
trying to understand how the whole thing fits together here). I expected
things to work similarly with ASP.Net but I just can't get it to work easily
for me..

I'm trying to carry around a Session("login_id") type variable & would like
to access it via code-behind (.aspx.vb) modules as well as class (.vb)
modules. I can successfully set a variable in a Page_Load event from one
form (during postback) but after re-directing to another form, I get a nasty
error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this
just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't need
to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP
Nov 18 '05 #1
9 2354
when you transfer to another page, it isn't gauranteed to occur on the same
calling thread so all your state information, including session is discarded
and a new session built up to service the transfer call. If you want to
return state, there is an overloaded function parameter which enables you to
do so. Server.Transfer(page, bPreserveForm) set this to true and you will be
alright. the default is false. Same deal for redirect.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl...
I'm having difficulty understanding Session state in ASP.Net. It's almost
embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use - perhaps I'm just not very smart or perhaps MS is making this too hard for us sql bunnies to understand - I
dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax. (also
very aware of scalability issues - so don't flame me on this - I'm just
trying to understand how the whole thing fits together here). I expected
things to work similarly with ASP.Net but I just can't get it to work easily for me..

I'm trying to carry around a Session("login_id") type variable & would like to access it via code-behind (.aspx.vb) modules as well as class (.vb)
modules. I can successfully set a variable in a Page_Load event from one
form (during postback) but after re-directing to another form, I get a nasty error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this
just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't need to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP

Nov 18 '05 #2
Thanks Alvin - but that didn't work.

I changed my Response.Redirect to a Server.Transfer(page, True) and I still
get the same error...

I noticed that the bPreserveForm argument seems only to apply to QueryString
& Form state. I want to preserve variable values in Session state, similarly
to the manner in which ASP Session worked. Is this possible in ASP.Net?
Perhaps there's simply some setting I'm not getting right or something..

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:eP**************@tk2msftngp13.phx.gbl...
when you transfer to another page, it isn't gauranteed to occur on the same calling thread so all your state information, including session is discarded and a new session built up to service the transfer call. If you want to
return state, there is an overloaded function parameter which enables you to do so. Server.Transfer(page, bPreserveForm) set this to true and you will be alright. the default is false. Same deal for redirect.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl...
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first

released
& it really shouldn't be this hard to use - perhaps I'm just not very

smart
or perhaps MS is making this too hard for us sql bunnies to understand - I dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax. (also very aware of scalability issues - so don't flame me on this - I'm just
trying to understand how the whole thing fits together here). I expected
things to work similarly with ASP.Net but I just can't get it to work

easily
for me..

I'm trying to carry around a Session("login_id") type variable & would

like
to access it via code-behind (.aspx.vb) modules as well as class (.vb)
modules. I can successfully set a variable in a Page_Load event from one
form (during postback) but after re-directing to another form, I get a

nasty
error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't

need
to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP


Nov 18 '05 #3
Try server.transfer(page.aspx, true).
Are you transferring to a page hosted in the application directory? That
should work otherwise the problem lies elsewhere.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:ef*************@tk2msftngp13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redirect to a Server.Transfer(page, True) and I still get the same error...

I noticed that the bPreserveForm argument seems only to apply to QueryString & Form state. I want to preserve variable values in Session state, similarly to the manner in which ASP Session worked. Is this possible in ASP.Net?
Perhaps there's simply some setting I'm not getting right or something..

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:eP**************@tk2msftngp13.phx.gbl...
when you transfer to another page, it isn't gauranteed to occur on the same
calling thread so all your state information, including session is

discarded
and a new session built up to service the transfer call. If you want to
return state, there is an overloaded function parameter which enables you to
do so. Server.Transfer(page, bPreserveForm) set this to true and you will
be
alright. the default is false. Same deal for redirect.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote
in message news:Oc**************@TK2MSFTNGP10.phx.gbl...
I'm having difficulty understanding Session state in ASP.Net. It's

almost embarrassing asking this as I've been using ASP since it was first

released
& it really shouldn't be this hard to use - perhaps I'm just not very

smart
or perhaps MS is making this too hard for us sql bunnies to understand - I
dunno, but I'd really appreciate someone explaining what I'm doing
wrong here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax.

(also very aware of scalability issues - so don't flame me on this - I'm just trying to understand how the whole thing fits together here). I expected things to work similarly with ASP.Net but I just can't get it to work

easily
for me..

I'm trying to carry around a Session("login_id") type variable & would

like
to access it via code-behind (.aspx.vb) modules as well as class (.vb) modules. I can successfully set a variable in a Page_Load event from one form (during postback) but after re-directing to another form, I get a

nasty
error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't

need
to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP



Nov 18 '05 #4
hmm - that's what I did..

Perhaps I'm stuffing something up in my config or somewhere else in my
code.. I'll keep on debugging & try to identify whatever.. :c/

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:#n**************@TK2MSFTNGP09.phx.gbl...
Try server.transfer(page.aspx, true).
Are you transferring to a page hosted in the application directory? That
should work otherwise the problem lies elsewhere.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:ef*************@tk2msftngp13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redirect to a Server.Transfer(page, True) and I

still
get the same error...

I noticed that the bPreserveForm argument seems only to apply to

QueryString
& Form state. I want to preserve variable values in Session state,

similarly
to the manner in which ASP Session worked. Is this possible in ASP.Net?
Perhaps there's simply some setting I'm not getting right or something..

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:eP**************@tk2msftngp13.phx.gbl...
when you transfer to another page, it isn't gauranteed to occur on the

same
calling thread so all your state information, including session is

discarded
and a new session built up to service the transfer call. If you want to return state, there is an overloaded function parameter which enables

you
to
do so. Server.Transfer(page, bPreserveForm) set this to true and you

will
be
alright. the default is false. Same deal for redirect.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote

in message news:Oc**************@TK2MSFTNGP10.phx.gbl...
> I'm having difficulty understanding Session state in ASP.Net. It's

almost
> embarrassing asking this as I've been using ASP since it was first
released
> & it really shouldn't be this hard to use - perhaps I'm just not very smart
> or perhaps MS is making this too hard for us sql bunnies to understand -
I
> dunno, but I'd really appreciate someone explaining what I'm doing

wrong > here & perhaps suggest a better approach..
>
> I'm familiar with use of the old Session("variable") = value syntax.

(also
> very aware of scalability issues - so don't flame me on this - I'm just > trying to understand how the whole thing fits together here). I expected > things to work similarly with ASP.Net but I just can't get it to work easily
> for me..
>
> I'm trying to carry around a Session("login_id") type variable & would like
> to access it via code-behind (.aspx.vb) modules as well as class (.vb) > modules. I can successfully set a variable in a Page_Load event from one > form (during postback) but after re-directing to another form, I get a nasty
> error when trying to access the value:
>
> Object reference not set to an instance of an object
> Line 29: Dim LoginID As String
> Line 30: LoginID = Session("LoginID").ToString
>
> Source File: ... home.aspx.vb Line: 30
>
> I've tried using HttpContext.Current.Session("LoginID").ToString but

this
> just gets the same problem.
>
> Why do I get an Object reference not set error? Surely Session doesn't need
> to be instanced does it?
>
> I'd appreciate any help.
>
> Regards,
> Greg Linwood
> SQL Server MVP
>
>



Nov 18 '05 #5
I think I may have an explanation:

I think that my code: Session("LoginID").ToString breaks if "LoginID" is not
in the HttpContext.Current.Session.Keys collection because there is no
object to call .ToString against.

Does this sound like a fair explanation? If so, how does one code
defensively around this issue without writing copious amounts of spag code
to handle session variables instances not being present?

Regards,
Greg Linwood
SQL Server MVP

"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:us*************@TK2MSFTNGP11.phx.gbl...
hmm - that's what I did..

Perhaps I'm stuffing something up in my config or somewhere else in my
code.. I'll keep on debugging & try to identify whatever.. :c/

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:#n**************@TK2MSFTNGP09.phx.gbl...
Try server.transfer(page.aspx, true).
Are you transferring to a page hosted in the application directory? That
should work otherwise the problem lies elsewhere.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:ef*************@tk2msftngp13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redirect to a Server.Transfer(page, True) and I still
get the same error...

I noticed that the bPreserveForm argument seems only to apply to

QueryString
& Form state. I want to preserve variable values in Session state,

similarly
to the manner in which ASP Session worked. Is this possible in ASP.Net? Perhaps there's simply some setting I'm not getting right or something..
Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in message news:eP**************@tk2msftngp13.phx.gbl...
> when you transfer to another page, it isn't gauranteed to occur on the same
> calling thread so all your state information, including session is
discarded
> and a new session built up to service the transfer call. If you want to > return state, there is an overloaded function parameter which enables you
to
> do so. Server.Transfer(page, bPreserveForm) set this to true and you

will
be
> alright. the default is false. Same deal for redirect.
>
> regards
>
> --
>
>
> -----------
> Got TidBits?
> Get it here: www.networkip.net/tidbits
> "Greg Linwood" <g_*********************************@hotmail.com >
wrote
in
> message news:Oc**************@TK2MSFTNGP10.phx.gbl...
> > I'm having difficulty understanding Session state in ASP.Net. It's
almost
> > embarrassing asking this as I've been using ASP since it was first
> released
> > & it really shouldn't be this hard to use - perhaps I'm just not very > smart
> > or perhaps MS is making this too hard for us sql bunnies to

understand -
I
> > dunno, but I'd really appreciate someone explaining what I'm doing

wrong
> > here & perhaps suggest a better approach..
> >
> > I'm familiar with use of the old Session("variable") = value
syntax. (also
> > very aware of scalability issues - so don't flame me on this - I'm

just
> > trying to understand how the whole thing fits together here). I

expected
> > things to work similarly with ASP.Net but I just can't get it to

work > easily
> > for me..
> >
> > I'm trying to carry around a Session("login_id") type variable & would > like
> > to access it via code-behind (.aspx.vb) modules as well as class

(.vb)
> > modules. I can successfully set a variable in a Page_Load event from one
> > form (during postback) but after re-directing to another form, I
get a > nasty
> > error when trying to access the value:
> >
> > Object reference not set to an instance of an object
> > Line 29: Dim LoginID As String
> > Line 30: LoginID = Session("LoginID").ToString
> >
> > Source File: ... home.aspx.vb Line: 30
> >
> > I've tried using HttpContext.Current.Session("LoginID").ToString
but this
> > just gets the same problem.
> >
> > Why do I get an Object reference not set error? Surely Session

doesn't > need
> > to be instanced does it?
> >
> > I'd appreciate any help.
> >
> > Regards,
> > Greg Linwood
> > SQL Server MVP
> >
> >
>
>



Nov 18 '05 #6
I too have been having problems with Session Variables at some servers--they
do not persist after, say, 20 seconds. But your problem is different.

And your code produces no problem when tested on my computer--the session
variable displays fine after a server.transfer or response.redirect.

What do you have in your web.config file as regards session state. Here is
what I have (watch word wrap).

<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password="
cookieless="false"
timeout="60"
/>

What is this "nasty error" you are receiving?
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl...
I'm having difficulty understanding Session state in ASP.Net. It's almost
embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use - perhaps I'm just not very smart or perhaps MS is making this too hard for us sql bunnies to understand - I
dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax. (also
very aware of scalability issues - so don't flame me on this - I'm just
trying to understand how the whole thing fits together here). I expected
things to work similarly with ASP.Net but I just can't get it to work easily for me..

I'm trying to carry around a Session("login_id") type variable & would like to access it via code-behind (.aspx.vb) modules as well as class (.vb)
modules. I can successfully set a variable in a Page_Load event from one
form (during postback) but after re-directing to another form, I get a nasty error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this
just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't need to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP

Nov 18 '05 #7
It will break with a null reference exception if login_id isn't there like
you said. Before touching session variables you should always test:
if(Session("login_id") != null) blah blah blah. But that doesn't explain why
it isn't there after the transfer in the first place. I've written a small
demo with a transfer and redirect. No matter how you transfer, session
variables will still be around unless you explicitly clear them. Are you
clearing it somewhere after the transfer? Otherwise, you will need to
examine your IIS settings adjusting the recycle properties as needed.

One more thing you can try is to put code in you session end event to
populate a static variable. Then try to read this variable on the page you
transferred to. Session end should never be called under these
circumstances, otherwise it is the reason for your lost session variables.

Regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:ur**************@TK2MSFTNGP10.phx.gbl...
I think I may have an explanation:

I think that my code: Session("LoginID").ToString breaks if "LoginID" is not in the HttpContext.Current.Session.Keys collection because there is no
object to call .ToString against.

Does this sound like a fair explanation? If so, how does one code
defensively around this issue without writing copious amounts of spag code
to handle session variables instances not being present?

Regards,
Greg Linwood
SQL Server MVP

"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:us*************@TK2MSFTNGP11.phx.gbl...
hmm - that's what I did..

Perhaps I'm stuffing something up in my config or somewhere else in my
code.. I'll keep on debugging & try to identify whatever.. :c/

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:#n**************@TK2MSFTNGP09.phx.gbl...
Try server.transfer(page.aspx, true).
Are you transferring to a page hosted in the application directory? That should work otherwise the problem lies elsewhere.

regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in message news:ef*************@tk2msftngp13.phx.gbl...
> Thanks Alvin - but that didn't work.
>
> I changed my Response.Redirect to a Server.Transfer(page, True) and I still
> get the same error...
>
> I noticed that the bPreserveForm argument seems only to apply to
QueryString
> & Form state. I want to preserve variable values in Session state,
similarly
> to the manner in which ASP Session worked. Is this possible in ASP.Net? > Perhaps there's simply some setting I'm not getting right or something.. >
> Regards,
> Greg Linwood
> SQL Server MVP
>
> "Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
> message news:eP**************@tk2msftngp13.phx.gbl...
> > when you transfer to another page, it isn't gauranteed to occur on the > same
> > calling thread so all your state information, including session is
> discarded
> > and a new session built up to service the transfer call. If you
want
to
> > return state, there is an overloaded function parameter which

enables you
> to
> > do so. Server.Transfer(page, bPreserveForm) set this to true and
you will
> be
> > alright. the default is false. Same deal for redirect.
> >
> > regards
> >
> > --
> >
> >
> > -----------
> > Got TidBits?
> > Get it here: www.networkip.net/tidbits
> > "Greg Linwood" <g_*********************************@hotmail.com >

wrote in
> > message news:Oc**************@TK2MSFTNGP10.phx.gbl...
> > > I'm having difficulty understanding Session state in ASP.Net. It's > almost
> > > embarrassing asking this as I've been using ASP since it was first > > released
> > > & it really shouldn't be this hard to use - perhaps I'm just not

very
> > smart
> > > or perhaps MS is making this too hard for us sql bunnies to
understand -
> I
> > > dunno, but I'd really appreciate someone explaining what I'm doing wrong
> > > here & perhaps suggest a better approach..
> > >
> > > I'm familiar with use of the old Session("variable") = value syntax. > (also
> > > very aware of scalability issues - so don't flame me on this - I'm just
> > > trying to understand how the whole thing fits together here). I
expected
> > > things to work similarly with ASP.Net but I just can't get it to

work
> > easily
> > > for me..
> > >
> > > I'm trying to carry around a Session("login_id") type variable &

would
> > like
> > > to access it via code-behind (.aspx.vb) modules as well as class (.vb)
> > > modules. I can successfully set a variable in a Page_Load event from one
> > > form (during postback) but after re-directing to another form, I get
a
> > nasty
> > > error when trying to access the value:
> > >
> > > Object reference not set to an instance of an object
> > > Line 29: Dim LoginID As String
> > > Line 30: LoginID = Session("LoginID").ToString
> > >
> > > Source File: ... home.aspx.vb Line: 30
> > >
> > > I've tried using HttpContext.Current.Session("LoginID").ToString

but > this
> > > just gets the same problem.
> > >
> > > Why do I get an Object reference not set error? Surely Session

doesn't
> > need
> > > to be instanced does it?
> > >
> > > I'd appreciate any help.
> > >
> > > Regards,
> > > Greg Linwood
> > > SQL Server MVP
> > >
> > >
> >
> >
>
>



Nov 18 '05 #8
Thanks Alvin.

I wasn't aware that you needed to do if(Session("login_id") != null) blah
blah blah, which I guess is a new thing you have to do in a strongly typed
environment. It makes sense, because I was testing
Session("login_id").Length which would give an object instance error if the
session variable wasn't there in the first place. Previously, in ASP, you
could check Len(Session("login_id")) which would be fine as any Variant had
a length. It's just a fundamental thinig I was missing. Because I didn't
realise this, I was not being overly careful how I populated the Session
variable in the first place, which was ultimately the source of the
problem - I had an XPATH query which was wrong & caused the
Session("loginid") variable to not be populated first..

Thanks for your help though!

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:uA**************@TK2MSFTNGP11.phx.gbl...
It will break with a null reference exception if login_id isn't there like
you said. Before touching session variables you should always test:
if(Session("login_id") != null) blah blah blah. But that doesn't explain why it isn't there after the transfer in the first place. I've written a small
demo with a transfer and redirect. No matter how you transfer, session
variables will still be around unless you explicitly clear them. Are you
clearing it somewhere after the transfer? Otherwise, you will need to
examine your IIS settings adjusting the recycle properties as needed.

One more thing you can try is to put code in you session end event to
populate a static variable. Then try to read this variable on the page you
transferred to. Session end should never be called under these
circumstances, otherwise it is the reason for your lost session variables.

Regards

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:ur**************@TK2MSFTNGP10.phx.gbl...
I think I may have an explanation:

I think that my code: Session("LoginID").ToString breaks if "LoginID" is not
in the HttpContext.Current.Session.Keys collection because there is no
object to call .ToString against.

Does this sound like a fair explanation? If so, how does one code
defensively around this issue without writing copious amounts of spag code
to handle session variables instances not being present?

Regards,
Greg Linwood
SQL Server MVP

"Greg Linwood" <g_*********************************@hotmail.com > wrote in message news:us*************@TK2MSFTNGP11.phx.gbl...
hmm - that's what I did..

Perhaps I'm stuffing something up in my config or somewhere else in my
code.. I'll keep on debugging & try to identify whatever.. :c/

Regards,
Greg Linwood
SQL Server MVP

"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in message news:#n**************@TK2MSFTNGP09.phx.gbl...
> Try server.transfer(page.aspx, true).
> Are you transferring to a page hosted in the application directory? That > should work otherwise the problem lies elsewhere.
>
> regards
>
> --
>
>
> -----------
> Got TidBits?
> Get it here: www.networkip.net/tidbits
> "Greg Linwood" <g_*********************************@hotmail.com > wrote in
> message news:ef*************@tk2msftngp13.phx.gbl...
> > Thanks Alvin - but that didn't work.
> >
> > I changed my Response.Redirect to a Server.Transfer(page, True)
and I > still
> > get the same error...
> >
> > I noticed that the bPreserveForm argument seems only to apply to
> QueryString
> > & Form state. I want to preserve variable values in Session state,
> similarly
> > to the manner in which ASP Session worked. Is this possible in

ASP.Net?
> > Perhaps there's simply some setting I'm not getting right or

something..
> >
> > Regards,
> > Greg Linwood
> > SQL Server MVP
> >
> > "Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
> > message news:eP**************@tk2msftngp13.phx.gbl...
> > > when you transfer to another page, it isn't gauranteed to occur
on
the
> > same
> > > calling thread so all your state information, including session
is > > discarded
> > > and a new session built up to service the transfer call. If you

want to
> > > return state, there is an overloaded function parameter which

enables
> you
> > to
> > > do so. Server.Transfer(page, bPreserveForm) set this to true and you > will
> > be
> > > alright. the default is false. Same deal for redirect.
> > >
> > > regards
> > >
> > > --
> > >
> > >
> > > -----------
> > > Got TidBits?
> > > Get it here: www.networkip.net/tidbits
> > > "Greg Linwood" <g_*********************************@hotmail.com >

wrote
> in
> > > message news:Oc**************@TK2MSFTNGP10.phx.gbl...
> > > > I'm having difficulty understanding Session state in ASP.Net. It's > > almost
> > > > embarrassing asking this as I've been using ASP since it was first > > > released
> > > > & it really shouldn't be this hard to use - perhaps I'm just not very
> > > smart
> > > > or perhaps MS is making this too hard for us sql bunnies to
> understand -
> > I
> > > > dunno, but I'd really appreciate someone explaining what I'm doing > wrong
> > > > here & perhaps suggest a better approach..
> > > >
> > > > I'm familiar with use of the old Session("variable") = value

syntax.
> > (also
> > > > very aware of scalability issues - so don't flame me on this - I'm > just
> > > > trying to understand how the whole thing fits together here). I > expected
> > > > things to work similarly with ASP.Net but I just can't get it to work
> > > easily
> > > > for me..
> > > >
> > > > I'm trying to carry around a Session("login_id") type variable & would
> > > like
> > > > to access it via code-behind (.aspx.vb) modules as well as class > (.vb)
> > > > modules. I can successfully set a variable in a Page_Load

event from
> one
> > > > form (during postback) but after re-directing to another form,
I get
a
> > > nasty
> > > > error when trying to access the value:
> > > >
> > > > Object reference not set to an instance of an object
> > > > Line 29: Dim LoginID As String
> > > > Line 30: LoginID = Session("LoginID").ToString
> > > >
> > > > Source File: ... home.aspx.vb Line: 30
> > > >
> > > > I've tried using
HttpContext.Current.Session("LoginID").ToString but
> > this
> > > > just gets the same problem.
> > > >
> > > > Why do I get an Object reference not set error? Surely Session
doesn't
> > > need
> > > > to be instanced does it?
> > > >
> > > > I'd appreciate any help.
> > > >
> > > > Regards,
> > > > Greg Linwood
> > > > SQL Server MVP
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #9
Hi William

I worked this out eventually - it was a Session variable population problem
& a fundamental mis-understanding about a strongly typed issue (which wasn't
a problem in ASP). My other post goes into a little further detail.

Thanks for your response though - my web.config is configured the same as
yours, but this isn't the problem after all.

Regards,
Greg Linwood
SQL Server MVP

"William LaMartin" <la******@tampabay.rr.com> wrote in message
news:#R**************@tk2msftngp13.phx.gbl...
I too have been having problems with Session Variables at some servers--they do not persist after, say, 20 seconds. But your problem is different.

And your code produces no problem when tested on my computer--the session
variable displays fine after a server.transfer or response.redirect.

What do you have in your web.config file as regards session state. Here is what I have (watch word wrap).

<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false"
timeout="60"
/>

What is this "nasty error" you are receiving?
"Greg Linwood" <g_*********************************@hotmail.com > wrote in
message news:Oc**************@TK2MSFTNGP10.phx.gbl...
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first

released
& it really shouldn't be this hard to use - perhaps I'm just not very

smart
or perhaps MS is making this too hard for us sql bunnies to understand - I dunno, but I'd really appreciate someone explaining what I'm doing wrong
here & perhaps suggest a better approach..

I'm familiar with use of the old Session("variable") = value syntax. (also very aware of scalability issues - so don't flame me on this - I'm just
trying to understand how the whole thing fits together here). I expected
things to work similarly with ASP.Net but I just can't get it to work

easily
for me..

I'm trying to carry around a Session("login_id") type variable & would

like
to access it via code-behind (.aspx.vb) modules as well as class (.vb)
modules. I can successfully set a variable in a Page_Load event from one
form (during postback) but after re-directing to another form, I get a

nasty
error when trying to access the value:

Object reference not set to an instance of an object
Line 29: Dim LoginID As String
Line 30: LoginID = Session("LoginID").ToString

Source File: ... home.aspx.vb Line: 30

I've tried using HttpContext.Current.Session("LoginID").ToString but this just gets the same problem.

Why do I get an Object reference not set error? Surely Session doesn't

need
to be instanced does it?

I'd appreciate any help.

Regards,
Greg Linwood
SQL Server MVP


Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: BravesCharm | last post by:
BravesCharm Dec 7, 10:57 am show options Newsgroups: microsoft.public.dotnet.distributed_apps From: "BravesCharm" <mastrauc...@gmail.com> Date: 7 Dec 2004 10:57:40 -0800 Local: Tues, Dec...
3
by: TPS | last post by:
Instead of using Hyperlinks and URL / Response.QueryString() parameters to pass state around in my app, I am using LinkButtons with a corresponding command event and command arguments. This page...
4
by: Jeff | last post by:
We have multiple ASP.Net web apps in development. As a standard we are looking to go with SQL Server to hold state information. Can we have the multiple apps all point to a single State DB? Or...
0
by: | last post by:
I recently wrote two asp.net based web apps based around the Enterprise Library application blocks and generated code and stored procedurescode in myGeneratation. One works fine but the other is...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
4
by: Joey | last post by:
Hello, I am having trouble with session state in my asp.net 1.1 C# web app. I am trying to program either a popup box warning of expiration, or I may do a redirect to the home page upon...
8
by: Andrew Teece | last post by:
Hope someone can help. We are trying to deploy an ASP.Net 2.0 application to a 3-node webfarm. The application uses the ReportViewer control in local mode, hence we need session state. Because we...
5
by: Laurahn | last post by:
Hi: i'm using modal window (showmodelessdialog) on asp. when the object is created i used session variables. After the object is closed the data is remaining on the modal window when is open...
4
by: DavidPr | last post by:
I'm using Jpmaster77 a.k.a. The Grandmaster of C++ 's register/login system and I'm having a problem with the session being dropped when I try to submit a form to the same page. A user has to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.