How do I pass session variables from one ASP page to another ASP page?

After developing extensive procedures to save form variables to session variables in a membership renewal process, I have discovered that Response.Redirect results in the session variables being lost. Well, they are not entirely lost, but I can only save a certain number of them. Based on research, it sounds like none of them should persist through multiple pages, so I am concerned that the results could be random. I found an article (http://weblogs.asp.net/bleroy/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_) that says an overloaded version of Redirect should be used (Response.Redirect("~/default.aspx", false);). Tried that and get:
Microsoft VBScript compilation  error '800a0414'
Cannot use parentheses when calling a Sub
/membership/joinRenew/membershipSession2.asp, line 2266
Response.Redirect("membershipSelect_R2.asp", false)
---------------------------------------------------^

Have also tried Server.Transfer which appears to successfully accomplish the transfer, but I get a different error. The first thing that occurs in the activated page is the opening of a text log file:
<%
Dim conn, rs, sql, alreadyExists, lastName, iTotal
dim whichname, whichdir, whichFN
dim filesys, textfile
dim memText1, memQty1, memAmt1, midx, iidx, temp

whichdir=Server.Mappath ("\membership\joinRenew")
whichname="\coreturn.html"
whichFN=whichdir & whichname
set filesys = Server.CreateObject("Scripting.FileSystemObject")
set textfile = filesys.OpenTextFile(whichFN, 8, 0)%>

textfile.WriteLine "
===============================

<%
Dim item1, quant1, amt1, item2, quant2, amt2, item3, quant3, amt3, item4, quant4, amt4, item5, quant5, amt5, item6, quant6, amt6, item7, quant7, amt7, item8, quant8, amt8, item9, quant9, amt9, item10, quant10, amt10, item11, quant11, amt11, item12, quant12, amt12, item13, quant13, amt13

item1                                = Session("sessitem_name1")
quant1                                = session("sessquantity1")
amt1                                 = session("sessamount1")
textfile.WriteLine Now
textfile.WriteLine "<BR><B>1:   </B>  " & (item1) & "     " & quant1 & "     " & amt1
textfile.WriteLine "<br>===============================<p>"...........................

Microsoft VBScript runtime  error '800a0046'
Permission denied
/membership/joinRenew/membershipSelect_R2.asp, line 13
(Line 13: set textfile = filesys.OpenTextFile(whichFN, 8, 0)%>)

This error doesn't occur when using Response.Redirect???
I've read everything I can find, and have yet to locate a solution. Is there one?
slegyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The session variable will be available from page to page as long as the worker process does not reset.  Just set the session then use it on subsequent pages.

PAGE 1
Session("sessitem_name1") = "abc123"


PAGE 2
response.write Session("sessitem_name1")


You may be better off to use an encrypted cookie though.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
fyi, your errors sound like something else is wrong.

You can also combine your session variables like this.


PAGE 1
x = "abc"
y = "123"
z = "xyz"
session("test") =x&"|"&y&"|"&z

PAGE2
arrayTEST = split(session("test"),"|")
x = arrayTEST(0)
y = arrayTEST(1)
z =arrayTEST(2)

response.write y
0
Big MontyWeb Ninja at largeCommented:
so the article you posted looks like it applied to asp .net, which is completely different from classic asp. in all of my experience, I've never had an issue passing session variables in the manner you are, so your problem isn't the sessions,.

it looks like you have a permissions issue. make sure your IUSR and NETWORK_SERVICES accounts have access to the folder you're trying to access.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
This piece of code is wrong
(Response.Redirect("~/default.aspx", false);). Tried that and get:
Microsoft VBScript compilation  error '800a0414' 
Cannot use parentheses when calling a Sub 
/membership/joinRenew/membershipSession2.asp, line 2266 
Response.Redirect("membershipSelect_R2.asp", false)

Open in new window

Loose the false and semi colon  Response.Redirect("membershipSelect_R2.asp") is all you need.

If I remember correctly you have a very large form.  If you end with all of those in a session, there is potential for problems and concatenating to one session can help.

This is the one that sounds like a permissions error
Microsoft VBScript runtime  error '800a0046' 
Permission denied 
/membership/joinRenew/membershipSelect_R2.asp, line 13 
(Line 13: set textfile = filesys.OpenTextFile(whichFN, 8, 0)%>)

Open in new window

0
slegyAuthor Commented:
I believe you are right about the article applying to asp.net. But I have found numerous questions on a variety of sites questioning why the values of session variables disappear.

I've tested until I'm blue in the face and can't seem to pinpoint where things are going awry. The code has been running successfully for nearly a year. I was recently asked to make a change, including increasing the number of items that could be purchased (and subsequently passed to PayPal).  By a lengthy and tedious process of elimination, I found that eight items work, but 9+ don't. I know that screams coding error, but I just don't see it.

VBScript that sets all the session variables - code added before Response.Redirect to verify content of variables:
item1                                = Session("sessitem_name1")
quant1                                = session("sessquantity1")
amt1                                 = session("sessamount1")
textfile.WriteLine Now
textfile.WriteLine "<BR><B>1:   </B>  " & item1 & "     " & quant1 & "     " & amt1
textfile.WriteLine "<br>===============================<p>"
.
.
.
item13                                = session("sessitem_name13")
quant13                                = session("sessquantity13")
amt13                                = session("sessamount13")
textfile.WriteLine Now
textfile.WriteLine "<BR><B>13:   </B>  " & item13 & "     " & quant13 & "     " & amt13
textfile.WriteLine "<br>===============================<p>"
Response.Redirect("membershipSelect_R2.asp")
Output:
2/27/2015 5:03:55 PM
1:  Active Membership with e-Flashes and Yearbook on CD 1 55.00
===============================
.
.
.
2/27/2015 5:03:55 PM
9:  Annual Operations Support Donation 1 50.00
===============================

2/27/2015 5:03:55 PM
10:  
===============================
.
.
.
The first thing that occurs in membershipSelect_R2 logs exactly the same information. The contents of all variables is null:
2/27/2015 5:03:55 PM
1:  
===============================

2/27/2015 5:03:55 PM
2:  
===============================

2/27/2015 5:03:55 PM
3:  
===============================
.
.
.
Are you saying that Response.Redirect should not have any impact on the contents of session variables?  Should Server.Transfer be a consideration?  All of the above referenced logging is made to the text file that is opened in each ASP page. Opening and writing to the file works when I use Response.Redirect. I got the  permission denied when on the log file when I tested with Server.Transfer.
0
slegyAuthor Commented:
Is there any clue in the fact that the session variables are correct before the Response.Redirect but empty when they are printed out immediately after the Response.Redirect?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
You have a lot of things here.  I would stick with response.redirect, you are dealing with one page knowing the other.  Your traffic is not very high so the argument that server.transfer is not really in play.

One gotcha with session variables is they can be lost.  Either you over loaded memory and crashed the worker process forcing it to refresh and start a new session or you went from http to  https or vice versa. Either could start a new session although there is a setting in iis that can allow you to keep the session.

You do have a lot of session variables.  I think you are better off keeping this in a cookie, then no worries about getting reset unless of course somebody resets their cookies in the middle of working with your site.

Also,  no need to keep item, qty and amount. You should only keep item and qty then calculate the amount when you need it.

One option to reduce the amount of session variables you store is to use a dictionary object.  http://www.w3schools.com/asp/asp_ref_dictionary.asp http://webcheatsheet.com/asp/dictionary_object.php
Let's say somebody added 3 of item 1, 1 of item 4 and 2 of item 10.  The hard code would look like
' --- Create Cart ----
Dim cart
Set cart=Server.CreateObject("Scripting.Dictionary")

' --- Add Items To Cart ----
cart.Add "item1",3
cart.Add "item4",1
cart.Add "item10",2

Open in new window

Then you can store your dictionary object in one session. Since you are using a fixed amount of form fields you could do

dim arrCart(12) ' 13 items
arrCart(0) = request.form("qty_item_1")
arrCart(1) = request.form("qty_item_2")
arrCart(2) = request.form("qty_item_3")
:
arrCart(13) = request.form("qty_item_12")

session("cart") = arrCart
' OR
response.cookie("cart") = arrCart

Open in new window

Now you are only storing 1 session variable.   To read it on the next page
arrCart(12) = session("cart")
response.write "You selected "&arrCart(0)&" of item 1"

Open in new window

To get the price you would do a look up.  If you only have 13 items, you could just put that recordset in a function and call it each time.  What I would typically do is throw those 13 items to a dictionary.  
Dim priceLookUp
Set priceLookUp=Server.CreateObject("Scripting.Dictionary")
' either hard code or loop through a recordset of data with the  name (or id) and price 
priceLookUp.Add "item1",15
priceLookUp.Add "item2",134
':
priceLookUp.Add "item13",82



response.write "You selected qty of "&arrCart(0)&" for item 1.  Your total is "&cdbl(arrCart(0))*cdbl(priceLookUp.item("item1"))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slegyAuthor Commented:
Thanks, Scott. As always, I appreciate your insight and help. I resurrected my subscription in the hope that the problem could be solved (I hate not knowing why something doesn't work). You are correct, I do create a large number of session variables. No matter what combination I test with, 8 total cart items is the maximum that will work. The good news is that in the past year, that number, as far as I know, was never exceeded. So 8 it will be. Again, I don't have time for a big rewrite, but in time I will try to change over to the dictionary object (I was not familiar with that). FYI, name, quantity and amount are needed, as that is the format that is passed to PayPal. Each item is passed separately, and PayPal does all the calculations.

Thank you again.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Good news!

I had an old cart doing the same thing by storing the dictionary object to a session.  I still ran into some issue with the session dropping and every once in a while that caused issues that I heard down the line.  Now I simply store the item number and qty in an encrypted cookie.  That way if they close the browser and come back 1 minute or 1 day later, the items could essentially still be in the cart.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.