Solved

Session.Abandon() for clearing out timer session variables

Posted on 2004-09-11
17
388 Views
Last Modified: 2012-06-27
BtF,

1.  The session.Abandon() function clears the session variables so they must login again directly after completing the quiz if they want to do anything else in the site (i.e take another quiz, same quiz, look at high scores, anything really......) Clicking on any link will redirect them to the login since there is no login username stored anymore.

How can I only clear the datTimeStart & datTimeFinish session variables and not my other session variables like: username, score, percent score......   It also prevents me from redirecting to another page after quiz is done and using all 8 session variables to write them to the DB.  After quiz done, all session variables are empty so I'm writing nothing to the DB.


2.  My last obstacle is trying to figure out how to prevent the user from refreshing the page after the results are shown because this will insert the record into the DB again and again.  Every time they refresh it inserts the record but I can't redirect when the quiz is done becuase it shows the results with the questions they got wrong and the correct answer so they can spend whatever time they want on the page looking over the questions.

Thanks.
0
Comment
Question by:kruegerste
  • 10
  • 7
17 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I already answered this in the other thread, but you can use the Session.Contents.Remove(strVariableName).

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
So, in your case, you would want this:

Session.Contents.Remove("datTimeStart")
Session.Contents.Remove("datTimeFinish")

FtB
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
And Number 3.  I can't find any timeout function in my ASP reference book.  If they take longer than 5 minutes on a question, I assume they walked away and I want a timeout error page to appear if they come back and click on anything.  I found in my book that the default is 20 minutes but I can't find out where to set it for lower.  
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Ah, you can set the script timeout like this:

<% Server.ScriptTimeout = 150 %>

where the parameter indicates the number of seconds.

FtB
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
Perfect.  All of your suggestions have worked wonderfully to no surprise.  Any suggestions for Number 2 (above) from the original post?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
That is indeed a tricky one.

I ordinarily execute a sql select against the database to see if the record already exists, and if not, add the new record. One other approach is to cause the page to go away on refresh by putting this near the top of your page:

<%
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>

Finally, you may want to separate the pages so that the insert happens in one page and the displaying of questions happens in another.

FtB
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I suppose that you could also create a session variable for the data insert--and do not allow a duplicate insert if  a value is set to the session variable.

FtB
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
I tried the code below and it seems to not change any of the shown output (the datTimeFinish value stays the same and doesn't retrieve the current Now()) but it still inserts the record again into DB.  Is there a redirect in this code below that will redirect them to another page if they hit the refresh?

<%
Response.Expires = 0
Response.Expiresabsolute = Now() - 1
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "no-cache"
%>

The easiest way would be to have them hit a "continue" submit button when they are done looking over the questions they got wrong which goes to an ASP which just inserts the record and then redirects to another page but my problem is: what if they don't hit the submit button when they are done, then their quiz record won't be inserted into DB.

The session variable you mentioned, would I just set it to some value (1) in the quiz script and then after the insert occurs set the value to something else (2) and only do the insert if the value of session variable is the initial value (1)?  How would this exactly work?

After I get this refresh problem done I will finally have this completed.
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 46

Accepted Solution

by:
fritz_the_blank earned 250 total points
Comment Utility
The above won't do anything to the session variables, but it should keep the page from resubmitting. However, given your situation, consider the following:

1) create the sql to update your record
2) create and populate a session variable called bolDataAdded

now, when you do the sql insert, you can do this:

if Session("bolDataAdded") ="" or IsEmpty(Session("bolDataAdded")) or isNull(Session("bolDataAdded")) then
     Session("bolDataAdded") = "Yes"
     'execute the sql insert here
end if

When the user is done and you begin a new quiz, just remove the session variable:

Session.Contents.Remove("bolDataAdded")

Fritz the Blank
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
Since I have been messing with this timer project I am now getting an error I never got before.  It is telling me that the "Close" method is not acceptable for closing the DB connection.  rsQuiz.Close() is receiving an error and I never got this error before.

How do I terminate the DB insert connection or don't I have to?

Set rsQuiz = Server.CreateObject("ADODB.Command")
rsQuiz.ActiveConnection = MM_DBConn_STRING
rsQuiz.CommandText = "Insert........"
rsQuiz.CommandType = 1
rsQuiz.CommandTimeout = 0
rsQuiz.Prepared = true
rsQuiz.Execute()
                  
rsQuiz.Close()
Set rsQuiz = Nothing
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Actually, you are not closing the connection, but rather, a command object which does not support the close method.

To close the the connection, I believe the syntax with DW should be:

MM_DBConn_STRING.Close()

If that is not right, please post the whole page so that I can see what is happening.

FtB

0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
MM_DBConn_STRING didn't work either.  Error I'm getting is:

Microsoft VBScript runtime error '800a01a8'

Object required: 'dsn=VLESQL;uid=sk116'

/116062/Website/Final Project/TMP74lwv3vyks.asp, line 499

Below here is some code from a recordset I create, open DB connection, retrieve information and then close DB connection.  This works fine and no errors.

' Retrieve question information from DB                        
Set rsQuiz = Server.CreateObject("ADODB.Recordset")
rsQuiz.ActiveConnection = MM_DBConn_STRING
rsQuiz.Source = "SELECT * FROM sk116062.Quest2 WHERE QuizID = 150 AND QuestionNumber=" & iQuestionNumber & ";"
rsQuiz.CursorType = 0
rsQuiz.CursorLocation = 2
rsQuiz.LockType = 1
rsQuiz.Open()      
                        
' Set the question information
strQuestionText = CStr(rsQuiz.Fields("QuestionText").Value)
                  
' Get an array of answers
aAnswers = Array( _
CStr(rsQuiz.Fields("AnswerA").Value & ""), _
CStr(rsQuiz.Fields("AnswerB").Value & ""), _
CStr(rsQuiz.Fields("AnswerC").Value & ""), _
CStr(rsQuiz.Fields("AnswerD").Value & ""))
                        
' Calculates wrong and right answers
For iGenericVariable = LBound(aAnswers) To UBound(aAnswers)
      If aAnswers(iGenericVariable) = "" Then
            ReDim Preserve aAnswers(iGenericVariable - 1)
                                Exit For
                End If
Next 'iGenericVariable

' Close and dispose of our DB object
rsQuiz.Close()
Set rsQuiz = Nothing

Now here is where I create a command for insert, open DB connection, execute insert and then try to close DB connectin but it won't.  It inserts the record into DB whether I have the Close() there or not.  It works without closing but I don't want this to freak out the server if I'm suppose to close it somehow.  

Here is all I'm trying to do when inserting the record:

--------------------------------------------------------------------------------------------------------
Set rsQuiz = Server.CreateObject("ADODB.Command")
rsQuiz.ActiveConnection = MM_DBConn_STRING

rsQuiz.CommandText = "INSERT INTO sk116062.HighScores(UserName, TotalCorrect, PercentScore) VALUES ('" & theuser & "','" & iTempScore & "','" & iScore & "')"

rsQuiz.CommandType = 1
rsQuiz.CommandTimeout = 0
rsQuiz.Prepared = true
rsQuiz.Execute()
            
MM_DBConn_STRING.Close()  ????
rsQuiz.Close()                        ????

'Then I clear the session Time variables after inserting their value in DB (haven't included this in the Insert yet though)
Session.Contents.Remove("datTimeStart")
Session.Contents.Remove("datTimeFinish")

----------------------------------------------------------------------------------------------------------------------------

These are the very last two things I do on the quiz page.  Do I need to close this connection?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
Okay, two things here:

1)rsQuiz was originally a recordset, so you could indeed call the close method. However, you do this later:

Set rsQuiz = Server.CreateObject("ADODB.Command")

So now rsQuiz is a command object, you cannot close it!

2) take a look at your connection include file and see if you can determine the name of your connection object.


FtB
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
I'm using a DSN so I'm not sure what my connection object would be.  

My inlcude file just looks like this:

Dim MM_DBConn_STRING
MM_DBConn_STRING = "dsn=VLESQL;uid=USERNAME;pwd=PASSWORD;"

Don't I close the ADODB.Command connection right after I insert the appropriate values?  When else would I do it?
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
You close recordset and connection objects only. For all objects, you set them to nothing.

So, for a recordset:

objRS.Close
set objRS = Nothing

For a command object:

set objCommand = Nothing

Do you not see a connection object being created anywhere?

FtB
0
 
LVL 4

Author Comment

by:kruegerste
Comment Utility
Thank you so much for your help.

I am done.  You are a life saver.

kruegerste
0
 
LVL 46

Expert Comment

by:fritz_the_blank
Comment Utility
I am glad to have helped,

FtB
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

11 Experts available now in Live!

Get 1:1 Help Now