Link to home
Start Free TrialLog in
Avatar of GRCHELPDESK
GRCHELPDESK

asked on

Changing Session Object Variables on the fly

Hello Experts,

So far I've been able to create a session and store the variables I'm looking for.
So my code looks something like this:

<%
Session("First_Name")= rsEmp("First_Name")
%>

Now with my application, the user will be clicking all kinds of buttons that can
affect what I'd like stored in these session variables.  Lets say for instance they
will change their display name.  Currently I'm using this code.

<%
Session("First_Name")= "Whatever"
%>

But that doesn't seem to be altering the value.  It just maintains its original value.
Am I doing something wrong?  Also, I should mention that it's a frames environment.  I
activate the session in the index page.  And later I alter the session in one of the frames.  
I'm not sure if that's the problem, but if so, what would be a good solution.

Thanks,
Maurice
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

As long as the frames are all part of the same domain, there should be no problem. However, if you update the value of a session variable in one frame, you must refresh the other frame before the change to the session variable will be reflected there.

FtB
SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, if you change the value of the session variable in one frame, and you want that change reflected in a different frame, you will need to refresh that different frame before the value will be reflected.

FtB
Avatar of GRCHELPDESK
GRCHELPDESK

ASKER

Hi Fritz,

If I understand you correctly, I think I currently have it set up to do what you're saying.  
But let me clarify that:

The first page to load is the index page, which sets up the session and loads the frames.
FRAME A | FRAME B
When the user clicks the button in frame A, it runs a Sub that makes the change
to the Session Variable, and then it submits the form to Frame B.  But when Frame B
reloads, it doesn't note the change.

Any thoughts?
Thanks!
Maurice
What happens if you right-click on frame B and choose refresh--do you see the change then?

FtB
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<%
Although very unadvicable to store recordsets ( or fields ) in the session object. THis should do it
Set Session("Recordset") = rsEmp

Sessions( "Recodset" ).Fields( "Firstname" ) = "What ever"
Sessions( "Recodset" ).Update

' Sessions( "Recodset" ).Fields( "Firstname" ) now contains "What ever"
%>
correction:
Sessions( "Recodset" ).Fields( "Firstname" ) = "What ever"
 should be
Sessions( "Recodset" ).Fields( "Firstname" ).Value = "What ever"

' Sessions( "Recodset" ).Fields( "Firstname" ).value now contains "What ever"
Or is you are only storing the first name in the session and want to alter that afterwards the code should be like this
Session("First_Name") = rsEmp.Fields("First_Name").Value

And now you have a string in the sesion whick can be altered.
Hey Guys,

Thanks for the feedback... sorry for taking a while to get back to you.
I'll answer some of those questions.

FtB - If I refresh Frame B, it still shows the old value.  As for building it without frames, that was my first thought as well.
Unfortunately the people I'm building it for don't see it that way.  :)

LaRell - I was thinking it might be browser caching as well.  So I added the following code:

<% Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1 %>

It still isn't working though.  I added that code into the index page, and both frames.

DaFou - It's not my intention to store an entire recordset in the Session.  (You're right, very bad)
I'm only taking a few select from the recordset to store in the session variables.  So I guess we
could reduce this question to say I currently have:

<%
Session("First_Name")= "WHATEVER"
%>

And when I try to reassign the value...

<%
Session("First_Name")= "SOMETHING ELSE"
%>

And then refresh the page, it doesn't accept the new value.  Very stubborned.

Thank-you guys, I appreciate all the feedback!
Maurice

I guess it is time for you to post some code so that we can see what is going on.

BTW, there isn't any chance that you are misspelling variable names, is there? Are you using Option Explicit?

FtB
Fair enough.  There's a lot of code, so I'll just post pieces from each frame.
Let me kwow if I left anything important out.

INDEX PAGE-

<%

' Prevent Browser Cashing issue
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

' Pull the user's windows ID and store it in var: strXPUser
Dim arrDomain, strXPUser
arrDomain = split(Request.ServerVariables("LOGON_USER"),"\")
strXPUser = arrDomain(1)

' Connect to grcw2k4045
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open = "DRIVER={SQL Server};SERVER=grcw2k4045;DATABASE=Daily_Briefing; User ID=bla; Password=bla;"

'3 table join, 1 - determines Emp Info 2 - Determines Dept Info
Set rsEmp = adoCon.Execute("Select e.Login_ID, e.First_Name, e.Last_Name, e.Default_Dept, c.Dept_Name From Dept c, Emp_Dept_XREF d, Emp e Where e.Login_ID = d.Login_ID AND d.Dept_ID = c.Dept_ID AND e.Login_ID =" & "'" & strXPUser & "'")    
                                               
' Setup Session Variables
Session("Login_ID")= rsEmp("Login_ID")
Session("First_Name")= rsEmp("First_Name")
Session("Last_Name")= rsEmp("Last_Name")
Session("CurrentDate")= date()
Session("strXPUser")=strXPUser
Session.Timeout=5

rsEmp.Close
Set rsEmp = Nothing
adoCon.Close
Set AdoCon = Nothing

%>

<frameset cols="255,*" framespacing="0" border="0" frameborder="0">
  <frame name="LeftFrame" target="main" src="menubar.asp" scrolling="auto" noresize>
  <frame name="RightFrame" target="_self" src="Briefing_Info.asp" scrolling="auto" noresize>
  <noframes>
  <body>
  <p>This page is setup for frames, and you're browser doesn't support them.</p>
  </body>
  </noframes>
</frameset>

FRAME A -

<%
' Again with the annoying browser caching issue
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>

<%
' Useless sub just to test changing session variables
sub SwapName()
session("strXPUser") = "Bubba"
end sub
%>

<form name="SearchForm" method="post" action="briefing_info.asp" target="RightFrame">
<input type="submit" name="submit" value="Change" onClick="SwapName()">
</form>

FRAME B -

<%
' Again with the annoying browser caching issue
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1

response.write session("strXPUser")
%>
is SwapName() geting called .. did u try a response.write within the function to verify that it is gettin called .. have been working with ASP.net for sometime now .. so a bit confused .. but in ASP when u write a onclick=functionname() .. doesn't it mean that the function has to be clientside function .. ur function is within server tags .. so i do not think it is even getting called .. i am sure u must be gettin a client error which might be gettin suppressed by ur browser .. just check the left side bottom corner of ur browser window (if u r using IE) for a error icon and double click it .. u might be gettin a type-mismatch error ....
Hi guys,

Rejojohnny - Thanks for the feedback.  I double-checked, but I'm not getting any errors.
I also just tried to use the same technique in the left frame without calling a function.  I just
re-assigned the value of the session object... and then manually refreshed the browser.  And it
still wasn't re-assigning it to the new value.  

Given that my deadline is fast approaching, and I don't have a solution for this I'm going to have
to run with an alternate solution.  I'm going to do a re-build in a frameless enviroment.  That'll
simplify a lot of things.  This was my back-up plan... but since FtB did suggest it, it seems fair to
give him the points on this one.  

Many thanks for contributing experts,
Maurice
no problem ... but just curious ... is the function gettin called ?? did u check it using some response.write statements ...
Hello All,

Correction, it was larellnielsen that recommended avoiding the frames.

ReJojohny - I believe you are correct... it doesn't seem to be calling the function.
How could I alter the code to make this work?

Thanks,
Maurice
Perhaps you might want to split points here so all that contributed get recognized? You can allocate points based on how much you think each person helped that way.

FtB
FtB - I've been thinking the same thing... that's why I haven't assigned the points yet.  :)
Thought so :-) now lets try this  .. remove the function and make changes as shown below .. also remove the function call from the submit button ..


FRAME A -

<%
' Again with the annoying browser caching issue
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
%>

<%
if len(request.form) > 0 then 'check if the form was submitted. If yes, then change the value
       session("strXPUser") = "Bubba"
end if
%>

<form name="SearchForm" method="post" action="briefing_info.asp" target="RightFrame">
<input type="submit" name="submit" value="Change">
</form>
It's still not changing the value... and no error messages.
... I'm at a bit of a loss... any thoughts?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, that seems to have changed the value... thank-you.
It's funny though... when I try to use logic to change the session variable it doesn't work.

ie: if len(request.form) > 0  or  IF request.form("Department") = "" then

There must be something about the way my page is submitting.  But I can get things
figured out from here.  Thanks for your help, it's much appreciated.

Cheers,
Maurice
You may need to do something like:

if request.form("Department") ="" or isEmpty(request.form("Department")) or isNull(request.form("Department")) then
   'do something
end if

FtB
>>session("strXPUser") = "Bubba"<<

Strange--I asked for something like that way up and the thread and it didn't make a difference then?

FtB
ur action is set to <form name="SearchForm" method="post" action="briefing_info.asp" target="RightFrame"> .. so i do not think ur page is calling itself .. so the code within the if condition does not get run ....

FTB,
>>Strange--I asked for something like that way up and the thread and it didn't make a difference then?
he might not have tried it .. :-)

that is y i just the kept asking him if the function was getting called when i was pretty sure, it might be getin called .. when he planned to close the question, i was curious whether my assumption was right or wrong .. so asked the question again .. anyway atleast he got it working now ...
Ya, I'm not sure what to tell you Fritz... I'm aware that you had made that suggestion earlier.
What I'm not sure of is why it wasn't working then, but it is now.  The code has seen some changes
since then... that could be it.  Or maybe it was an error on my part.  Regardless, I'm sure you all can
appreciate that this makes it a little challenging to reward the points.  

Part points to FtB since he actually suggested the final solution first.
Part points to Rejojohny since he helped me find the error with the sub, and helped get the page working.
Part points also to larellnielsen since he adviced me to build the app without frames, and
that's the approach I've ended up taking because of time issues.

I appreciate everyone's input, and sorry about the confusion.
Maurice
That is all fine with me. If you think there are too few points to go around, you can leave me out of it and pass my share on.

Best of luck with your project,

FtB