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:uAcvTkRsDHA.3468@TK2MSFTNGP11.phx.gbl...[color=blue]
> 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[/color]
why[color=blue]
> 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_linwoodremovethisbeforeemailingme@hotmail.com > wrote in
> message news:urAKzoNsDHA.2360@TK2MSFTNGP10.phx.gbl...[color=green]
> > I think I may have an explanation:
> >
> > I think that my code: Session("LoginID").ToString breaks if "LoginID" is[/color]
> not[color=green]
> > 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[/color][/color]
code[color=blue][color=green]
> > to handle session variables instances not being present?
> >
> > Regards,
> > Greg Linwood
> > SQL Server MVP
> >
> > "Greg Linwood" <g_linwoodremovethisbeforeemailingme@hotmail.com > wrote[/color][/color]
in[color=blue][color=green]
> > message news:uscsIZNsDHA.560@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > 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[/color][/color]
> in[color=green][color=darkred]
> > > message news:#n#vhfMsDHA.1996@TK2MSFTNGP09.phx.gbl...
> > > > Try server.transfer(page.aspx, true).
> > > > Are you transferring to a page hosted in the application directory?[/color][/color]
> That[color=green][color=darkred]
> > > > should work otherwise the problem lies elsewhere.
> > > >
> > > > regards
> > > >
> > > > --
> > > >
> > > >
> > > > -----------
> > > > Got TidBits?
> > > > Get it here:
www.networkip.net/tidbits
> > > > "Greg Linwood" <g_linwoodremovethisbeforeemailingme@hotmail.com >[/color][/color][/color]
wrote[color=blue][color=green]
> > in[color=darkred]
> > > > message news:efMJYVMsDHA.640@tk2msftngp13.phx.gbl...
> > > > > Thanks Alvin - but that didn't work.
> > > > >
> > > > > I changed my Response.Redirect to a Server.Transfer(page, True)[/color][/color][/color]
and[color=blue]
> I[color=green][color=darkred]
> > > > 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[/color]
> > ASP.Net?[color=darkred]
> > > > > Perhaps there's simply some setting I'm not getting right or[/color]
> > something..[color=darkred]
> > > > >
> > > > > Regards,
> > > > > Greg Linwood
> > > > > SQL Server MVP
> > > > >
> > > > > "Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com >[/color][/color]
> wrote[color=green][color=darkred]
> > > in
> > > > > message news:ePvc9KMsDHA.2304@tk2msftngp13.phx.gbl...
> > > > > > when you transfer to another page, it isn't gauranteed to occur[/color][/color][/color]
on[color=blue][color=green]
> > the[color=darkred]
> > > > > same
> > > > > > calling thread so all your state information, including session[/color][/color][/color]
is[color=blue][color=green][color=darkred]
> > > > > discarded
> > > > > > and a new session built up to service the transfer call. If you[/color][/color]
> want[color=green][color=darkred]
> > > to
> > > > > > return state, there is an overloaded function parameter which[/color]
> > enables[color=darkred]
> > > > you
> > > > > to
> > > > > > do so. Server.Transfer(page, bPreserveForm) set this to true and[/color][/color]
> you[color=green][color=darkred]
> > > > will
> > > > > be
> > > > > > alright. the default is false. Same deal for redirect.
> > > > > >
> > > > > > regards
> > > > > >
> > > > > > --
> > > > > >
> > > > > >
> > > > > > -----------
> > > > > > Got TidBits?
> > > > > > Get it here:
www.networkip.net/tidbits
> > > > > > "Greg Linwood" <g_linwoodremovethisbeforeemailingme@hotmail.com >[/color]
> > wrote[color=darkred]
> > > > in
> > > > > > message news:OcFr9EMsDHA.1884@TK2MSFTNGP10.phx.gbl...
> > > > > > > I'm having difficulty understanding Session state in ASP.Net.[/color][/color]
> It's[color=green][color=darkred]
> > > > > almost
> > > > > > > embarrassing asking this as I've been using ASP since it was[/color][/color]
> first[color=green][color=darkred]
> > > > > > released
> > > > > > > & it really shouldn't be this hard to use - perhaps I'm just[/color][/color][/color]
not[color=blue][color=green][color=darkred]
> > > 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[/color][/color]
> doing[color=green][color=darkred]
> > > > wrong
> > > > > > > here & perhaps suggest a better approach..
> > > > > > >
> > > > > > > I'm familiar with use of the old Session("variable") = value[/color]
> > syntax.[color=darkred]
> > > > > (also
> > > > > > > very aware of scalability issues - so don't flame me on this -[/color][/color]
> I'm[color=green][color=darkred]
> > > > just
> > > > > > > trying to understand how the whole thing fits together here).[/color][/color][/color]
I[color=blue][color=green][color=darkred]
> > > > expected
> > > > > > > things to work similarly with ASP.Net but I just can't get it[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > work
> > > > > > easily
> > > > > > > for me..
> > > > > > >
> > > > > > > I'm trying to carry around a Session("login_id") type variable[/color][/color][/color]
&[color=blue][color=green][color=darkred]
> > > would
> > > > > > like
> > > > > > > to access it via code-behind (.aspx.vb) modules as well as[/color][/color]
> class[color=green][color=darkred]
> > > > (.vb)
> > > > > > > modules. I can successfully set a variable in a Page_Load[/color][/color][/color]
event[color=blue][color=green]
> > from[color=darkred]
> > > > one
> > > > > > > form (during postback) but after re-directing to another form,[/color][/color][/color]
I[color=blue][color=green]
> > get[color=darkred]
> > > 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[/color][/color][/color]
HttpContext.Current.Session("LoginID").ToString[color=blue][color=green]
> > but[color=darkred]
> > > > > 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
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]