View Full Version : ASP losing session variables?
SilverTiger
07-02-2003, 04:29 PM
I seem to be losing session variables. hre are the facts:
1. I have created a page that has various forms.
2. each form is part of a sub
3. the forms have an action of the asp page itself so it reloads
4. upon completion of each sub the pageaction changes
5. pageaction determins the sub to run.
one of these subs sets many session variables to help define a seatch of a database.
when I perform this sub it defines the variables properly, but after performing another sub and reloading i loose my session variables.
I have tried displaying the sessionID, and it does not change as i navigate this "search.asp" page. the page goes throught he subs properly, and even retains other session variables, but not the ones that in this one specific sub.
i dont have any "clear" or "delete" commands regarding session variables, but still, they dissapear..
any ideas?
Welcome here,
session-values don't disappear, so theres probably something wrong with you code :D
That's all we can say without seeing your code.
SilverTiger
07-02-2003, 06:17 PM
there is a form in one of the subs with a action of the current asp page and a pageaction variable set to "ProcessInfo"
the code used to set the variables inside "sub ProcessInfo()" is as follows:
Session("Subject_FirstName") = Request.Form("FirstName")
Session("Subject_LastName") = Request.Form("LastName")
Session("Subject_DOB_Month") = Request.Form("DOB_Month")
Session("Subject_DOB_Day") = Request.Form("DOB_Day")
Session("Subject_DOB_Year") = Request.Form("DOB_Year")
Session("Subject_DOB") = Session("Subject_DOB_Month") + "/" + Session("Subject_DOB_Day") + "/" + Session("Subject_DOB_Year")
Session("Subject_SSN_1") = Request.Form("SSN_1")
Session("Subject_SSN_2") = Request.Form("SSN_2")
Session("Subject_SSN_3") = Request.Form("SSN_3")
Session("Subject_SSN") = Session("Subject_SSN_1") + "-" + Session("Subject_SSN_2") + "-" + Session("Subject_SSN_3")
these session variables are then referred to later in the script, but when i put in code to display them... i.e.:
response.write("the session variable for First Name is : " & Session("FirstName") & ".")
I get no value, just a blank
but when i use:
response.write("the session ID is: " & Session(Session.SessionID) & ".")
it displays the correct session, which corrosponds to the previos asp load of the Search page.
SilverTiger
07-02-2003, 06:19 PM
the line of script used to display them was actually:
response.write("the session variable for First Name is : " & Session("Subject_FirstName") & ".")
SilverTiger
07-02-2003, 06:55 PM
here is the asp file in tct format. if you follow the logic, the session variables are set when the sub "processInfo" is called, but when referred to later in the ASP script (i.e. "Display Preorder") the variables do not show up.
I do not understand why?
arnyinc
07-02-2003, 07:24 PM
Insert some response.write commands in there to error check. Specifically, in your sub processinfo() write the values of the session variable right after you set it.
Session("Subject_FirstName") = Request.Form("FirstName")
response.write Session("Subject_FirstName") & " - " Request.Form("FirstName")
Make sure those aren't blank. If those are equal, keep going and test it later on. At some point, it must be submitting a form where the values for all the boxes are empty so it will accidentally clear out the values of all your session vars.
I don't see anything wrong with the code to set or display the sessionvariabels.
Maybe restart your IIS. I've sometimes had strange sessionissues that disappeared after restarting the IIS.
Or check if the session didn't timout (cause i don't think your printing the sessionID, since it should then be
session.sessionID ) --> close all browserwindows and then open a new window and return to your app.
I'm also not sure about the underscores in the variabelnames + you use way to much sessionvariabels
Don't take this wrong, but i don't think that your code is efficient. There's no point is storing all that data into sessionvariabels. Certainly not if you first select them from a db, or if they are still in the forms-collection. I mean, you store them first in a session-variabel and on the same page you use the value from the sessionvariable.
Sessionvariabels are verry easy to work with, but should only be used for info that needs to be used on multimple pages and that can't be pulled from the database, or can't be stored in a hidden formfield.
And your 'control of flow' could also be better...
Just my opinion.
SilverTiger
07-02-2003, 08:18 PM
I originally had this in 4 or 5 individual asp pages, but condensed it onto one to actually increase efficiency... but I agree about the flow.
the forms are created from field types in a database, which are stored in session variables. Then the forms are filled in with data and submitted to session variables that will be written into a orders database.
what method would you recommend to keep the data active between reloads of the same asp page besides Session variables? I could use application variables, but isn't that basically the same idea since I am using only one asp page?
any pointers would be greatly appreciated.
Certainly not application-variabels --> they should only contain variabels with 'application-scope', data that should be the same for all session (like number of online-users etc)
What i would suggest:
- if you post the form to itself, then the data is in the forms collection, and you can just read it from there and, if necessary, store them in a hidden textfield***
- after validating the data, you can insert them into the db. You'll notmally also insert some sort of userID or other unique identifyer (not the ASP sessionID, since this one isn't unique). You normally then store that userID or a databasegenerated session-identifier into a sessionvariabel.
- on the following pages, you can then just select all the data again, using the identifier from the session-variabel in the condition. You know;
sql="select * from table where sesID=" & session("dbsessionkeyID")
***like this
dim load, name, problem
problem=0
if Len(request.form("load"))=0 then 'this means: on first load
response.write("Fill in the form. Please.")
load = 0
else
load = request.form("load")
end if
'now check the formfieldsvalues and display the form, with appropriate feedback before the formfields with invalid values
if load = 0 then
name="" 'or something like "Your name ..." biu this complicates the display
else
name=request.form("name")
if Len(name) < 4 then
respons.write("You didn't enter a valid name.<br />")
problem = problem + 1
end if
end if
if Len(name) < 4 then 'so also on first load, the textbox will be displayed, but empty
response.write("Name = <input type=""text"" value=""" & name & """ name=""name"">")
else
response.write("<input type=""hidden"" value="" & name & "" name=""name"">")
end if
... same for the other fields.
' after all checks, maybe do the insert (if there were no invalid values
if load >1 and problem = 0 then
%>
<!--#include file='conDB.asp'-->
<%
sql="insert blabla"
else
response.write("<input type=""hidden"" value=""1"" name=""load"">")
end if
You see? Just reading the flag to determine if the page was already loaded before, then validate the values --> if invalid, display message and display the filled in form with errormessage or a red arrow or so on the incorrect values. If they are valid, you include the value in a hidden textfield (and exclude it from the form, or you could make it easier on yourself and just display the value in the textfield, which would save you a few dozen lines of code). if all values are valid, open connection and insert. On first load, just display the form.
If you need the dat on the next page, you can ad it to the querystring when redirecting, or you can pull it out of the db on the next page, or you could include that pages code in this file, rigt after the insert (and just use the values from the forms-collection)
How your script looks of coarse depends on the kind of form you've got, and the kind of checks that you perform. You could set up functions to check stringvalues, integers, floats etc. Or choose meaningful fieldnames so that you can use the filenames to build the errormessages etc etc
If you run some searches on multiple-purpose pages, you'll find plenty of tutorials and sample code.
Or just ask us here, if you have some questions or problems.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.