Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Session.Abandon / Session_End not working

Status
Not open for further replies.

millerk

Programmer
Jul 5, 2002
133
US
I have an app that generates crystal reports and exports them to different file formats. I am using the SessionID in the name of the file created to maintain unique file names between different users. I want to be able to delete these files when the user's session ends or they close their browser. I tried putting the code in the Session_End event and the Application_End events of Global.asax, but neither of those seem to be firing. For testing, I put a Session.Abandon in a click event for a button and that doesn't seem to work either. I have the same SessionID before and after clicking the button. Does anyone have any ideas about why this isn't working? Or is there another event I can use that will always occur? I need to be able to delete those files when the users are done with them.

Thanks
 
Hey Millerk,

First off, quick lesson about session_OnEnd:
The session_OnEnd does NOT fire when the browser is closed. On the webserver, there is a setting you can set for how long a session can live without any interaction (ie. requesting pages, etc.). So if you have that set to, lets say, an hour, then the session_onend won't fire until an hour from the moment your user stops doing anything on your site.

Its still a good idea to set the session_onend to do any final clean up, but it isn't something that you can access from a button click, etc.

So if you put the code in there, eventually the files will be deleted (it may just take a while). But what if you want the files to be deleted when the user closes the browser or goes to a different page? That takes a little bit of javascript, but its not that hard.

In either your page's body tag or frameset's main frameset tag, you can specify what to do "onunload". This event fires when the user closes the browser or browses to anotehr page. Here's how we used both the "onunload" and the code behind to handle logout of our portal:

If the user closes the browser, then the onunload opens a new window (a small new window). This is a logoff aspx page, that has code in its page_load function that clears out the login information from teh database. In your case, it would delte files. In that page's onload event (in the html), I have a window.close() so the window closes once its done.

In case that buggers up, or they are just inactive, the Session_OnEnd code does pretty much the same thing, just later.

Sorry about the length, and I hope I was clear. Let me know if you have any other questions.
:)

jack

 
I understand about the session timeout, but shouldn't Session.Abandon end the session immediately and fire the Session_End event?

Using the onunload sounds like it might work. The problem I see is that I can't delete the file when they leave my main page. The way it works is that the user selects a report and a file format from dropdown lists, then clicks on a button to run the report. After the report has been processed and exported to a file, I display a link to the file. The user will normally just click on this link to get the file. If I delete this file from the onunload event of my page, it will be gone when they try to go to it. And because the link is to the exported file and not to an html or asp page, I don't see how I can add any javascript code to that file.

Is the Session_End event reliable? In other words, can I count on it always firing? If so, having the code there would work. Waiting 20 minutes for the files to be deleted would not be a problem as long as they did always get deleted.

Thanks for your help on this. I've been programming for awhile in VB 6.0 and done a little ASP, but I'm very new to ASP.Net
 
Ahh, ok.

From our tests, yes: the session_onEnd is reliable and will fire. The only maintenance it needs is a sensible timeout property.

One thing just to throw out (since we may be picking up Crystal to use in the near future for our web app):
Instead of running the report and saving it to a file, couldn't you run the report, but output it to the browser right away (that way not needing to save the file locally at all)? Or do the users need the ability to click the link for htem?

Jack
 
Maybe it's something in my configuration, but the session_End event doesn't seem to be firing. I have code that deletes the file. I have tested it using a button click event and it works. I then put that code in the session_end event handler and set the session timeout to 2 minutes. I verified that the timeout was 2 minutes by displaying the Session.Timeout property when a button is clicked. I then close my browser and wait and nothing happens.

For my app the users need to have the choice of what format they want the report in. If they just want to view it, it's no problem. I have a crystal report viewer control and I just create the report object and set it as the report source for the viewer. If they want the report as an Excel or PDF file, I don't know of any way to do that without creating a local file. Even if I automatically redirect them to the file, I'm still creating a local file. The reason I give them a link is that automatically redirecting gives an error that the file is corrupt or gives a blank screen instead of opening the file. I even tried putting a delay in before redirecting to make sure the file was completely written, but I still got the same error. Either way, I still have to create a local file.

Anyone have ideas why the session_end event is not firing? If I can't figure that out, I may do the cleanup in the page_load event. I could check the creation dates of the files and delete all the ones that are older than 20 minutes. This way we would always have a few files there, but it wouldn't be too many.

Thanks for any ideas.
 
OK, I just removed the file deletion code from my Session_End event handler and added code to write a test text file. Now Session.Abandon DOES work and the Session_End event DOES fire. The file deletion code is the same code that worked fine in a button click event and it compiles fine, but when it is put in the Session_End event, none of the code in that event runs. Here's the code:
Dim exportpath As String
Dim f As New FileInfo(Server.MapPath("MyPage.aspx"))
exportpath = f.DirectoryName & "\" & Session.SessionID & ".xls"
'System.IO.File.Delete(exportpath)
Dim objWriter As New StreamWriter("C:\testFile.txt", True)
objWriter.WriteLine("---------")
objWriter.WriteLine("This came from the Session_End event")
objWriter.WriteLine("SessionID: " & Session.SessionID)
objWriter.WriteLine("ExportPath: " & exportpath)
objWriter.WriteLine("---------")
objWriter.Close()

Any ideas why this wouldn't work.
 
The problem was Server.MapPath. I guess I can't use that in the Session_End event and I wasn't getting any error messages since there wasn't anywhere to display them. I tracked down the problem using try / catch statements and writing the error information to a text file.
Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top