473,835 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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 2390
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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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******** *****@tk2msftng p13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString
>
> Source File: ... home.aspx.vb Line: 30
>
> I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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("LoginI D").ToString breaks if "LoginID" is not
in the HttpContext.Cur rent.Session.Ke ys 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******** *****@TK2MSFTNG P11.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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
Thanks Alvin - but that didn't work.

I changed my Response.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString
> >
> > Source File: ... home.aspx.vb Line: 30
> >
> > I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng
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.redire ct.

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

<sessionState
mode="InProc"
stateConnection String="tcpip=1 27.0.0.1:42424"
sqlConnectionSt ring="data source=127.0.0. 1;user id=sa;password= "
cookieless="fal se"
timeout="60"
/>

What is this "nasty error" you are receiving?
"Greg Linwood" <g_************ *************** ******@hotmail. com> wrote in
message news:Oc******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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("log in_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******** ******@TK2MSFTN GP10.phx.gbl...
I think I may have an explanation:

I think that my code: Session("LoginI D").ToString breaks if "LoginID" is not in the HttpContext.Cur rent.Session.Ke ys 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******** *****@TK2MSFTNG P11.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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
> Thanks Alvin - but that didn't work.
>
> I changed my Response.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString
> > >
> > > Source File: ... home.aspx.vb Line: 30
> > >
> > > I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng

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("log in_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("lo gin_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("logini d") 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******** ******@TK2MSFTN GP11.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("log in_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******** ******@TK2MSFTN GP10.phx.gbl...
I think I may have an explanation:

I think that my code: Session("LoginI D").ToString breaks if "LoginID" is not
in the HttpContext.Cur rent.Session.Ke ys 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******** *****@TK2MSFTNG P11.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******** ******@TK2MSFTN GP09.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******** *****@tk2msftng p13.phx.gbl...
> > Thanks Alvin - but that didn't work.
> >
> > I changed my Response.Redire ct 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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString
> > > >
> > > > Source File: ... home.aspx.vb Line: 30
> > > >
> > > > I've tried using
HttpContext.Cur rent.Session("L oginID").ToStri ng 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******@tampa bay.rr.com> wrote in message
news:#R******** ******@tk2msftn gp13.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.redire ct.

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

<sessionState
mode="InProc"
stateConnection String="tcpip=1 27.0.0.1:42424"
sqlConnectionSt ring="data source=127.0.0. 1;user id=sa;password= " cookieless="fal se"
timeout="60"
/>

What is this "nasty error" you are receiving?
"Greg Linwood" <g_************ *************** ******@hotmail. com> wrote in
message news:Oc******** ******@TK2MSFTN GP10.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("variab le") = 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("LoginI D").ToString

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

I've tried using HttpContext.Cur rent.Session("L oginID").ToStri ng 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
3238
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 7 2004 10:57 am Subject: Have trouble with reference counts! Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
3
1514
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 has LinkButtons in a menu down the left hand side, that when clicked causes the main content of the page to change. The main content of the page has linkbuttons as well. I am having trouble with the linkbuttons in the maincontent area keeping...
4
2334
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 do we need to install multiple instances of the State database on to the SQL Server (one for each Web app)? It would seem that you could run into troubles pointing several web apps at the same DB, because you would introduce the possibility (a...
0
913
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 unusabe on that server as it suffers frequent random losses of session state. Both apps are currently only used by less than 10 people. I found that if I copied the problem one to our development server it works but it will not work on the live...
6
3813
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 one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
4
1130
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 expiration. But I can't seem to get started, because I am getting values that I do not understand. In IIS settings, I stopped my website/app and went into the properties for it and selected the "Home Directory" tab. Then I clicked the "Configuration"...
8
5365
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 dont wan't a single point of failure we use SQL Session State. I have setup a persistant sql session state database on a SQL 2000 server (this is a cluster in production, so no SPOF) I have setup our web.config files to include; " <sessionState...
5
1271
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 again, even if i clear all session variables.
4
1390
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 register and login to access the form. The information on the form is suppose to be entered into a database and the user given an all OK message, with the user options shown at the top. The form I'm using is a copy of a working form with only the...
0
9803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4433
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.