Solved

Changing Session Object Variables on the fly

Posted on 2004-09-10
27
211 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:GRCHELPDESK
  • 9
  • 9
  • 5
  • +2
27 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 
LVL 46

Assisted Solution

by:fritz_the_blank
fritz_the_blank earned 165 total points
Comment Utility
So let's say, for example, you have two frames, top and bottom, and the Session("First_Name")= "Some Previous Value." Further, let's say this happens in some code for the bottom frame:

Session("First_Name")= "Some new value from the bottom frame"

But this in the top frame:

Response.write(Session("First_Name"))

The top frame will continue to write out "Some Previous Value" until such time the top frame is refreshed, at which point it will display "Some new value from the bottom frame"


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
What happens if you right-click on frame B and choose refresh--do you see the change then?

FtB
0
 
LVL 2

Assisted Solution

by:larellnielsen
larellnielsen earned 165 total points
Comment Utility
Is the URL that you are loading back into Frame B the same as URL that was initially loaded into Frame B?

If so you are running into the famous browser caching issue.

Try adding Response.Expire = -1 to the script which should expire the response content immediate and not cache it on the browser.

On a final note, is there any way to build your site without using frames? If so, that would be the route that I would take. The reason is that dynamic content and frames do not play very nice together. Do not get me wrong frames are cool :o) they can just be pains sometimes.

LaRell
0
 
LVL 2

Expert Comment

by:DaFou
Comment Utility
<%
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"
%>
0
 
LVL 2

Expert Comment

by:DaFou
Comment Utility
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"
0
 
LVL 2

Expert Comment

by:DaFou
Comment Utility
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.
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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

0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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")
%>
0
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
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 ....
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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
0
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
no problem ... but just curious ... is the function gettin called ?? did u check it using some response.write statements ...
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
FtB - I've been thinking the same thing... that's why I haven't assigned the points yet.  :)
0
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
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>
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
It's still not changing the value... and no error messages.
... I'm at a bit of a loss... any thoughts?
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 170 total points
Comment Utility
try this

<%
       session("strXPUser") = "Bubba"
%>
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
>>session("strXPUser") = "Bubba"<<

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

FtB
0
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
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 ...
0
 
LVL 2

Author Comment

by:GRCHELPDESK
Comment Utility
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
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now