locked
Can ASP.net code be persistent throughout the web user's session? RRS feed

  • Question

  • User-1143177497 posted

    I am building a page that opens a copy of an Excel file, insert user inputs onto a Worksheet, some calculation is done by Excel, and then ASP exports another Worksheet with calculation results to the web user.

    However instead of opening the Workbook and closing it immediately, I want the Excel Workbook to remain open for the duration of the user's session, so the user can update 5~10 different parameters, and see result calculated by Excel.

    How can this be achieved? Right now I am connecting to Excel thru Microsoft.Office.Interop.Excel.Application

    I understand that this is not the ideal solution, but it's a given requirement, and the project is not large scale, there will just be 3~4 users.

    Thanks,

    GC

    Sunday, July 5, 2015 10:20 PM

Answers

  • User-821857111 posted

    But then how do I trigger a code to Close the Workbook if the user went MIA and Session expired?
    You can use the Session_End event in global.asax to execute code when a session ends. It only works for InProc sessions (the default kind).

     I'll look into how to save the object in Session variable

    // saving to session variable
    Session["workbook"] = workBook;
    
    //retrieving from Session variable
    var workBook = (Excel.WorkBook)Session["workbook"];




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2015 4:50 AM
  • User-821857111 posted

    You can add the event handler to global.asax:

    Sub Session_End(sender As Object, e As EventArgs)
    
    End Sub

    You add a workbook to a session variable wherever you need to (usually in your code behind). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2015 10:34 AM

All replies

  • User-821857111 posted

    You can save the object that represents the workbook in a session variable.

    Monday, July 6, 2015 2:42 AM
  • User-1143177497 posted

    Thanks for that idea, I'll look into how to save the object in Session variable (as I'm a noob in ASP)

    So my ASP code will leave Excel Workbooks (one copy for each user Session) lying around on the server. But then how do I trigger a code to Close the Workbook if the user went MIA and Session expired?

    If the user completed the action (e.g they did some action to close the case in the front-end) I can trigger closing Excel manually.

    Monday, July 6, 2015 4:35 AM
  • User-821857111 posted

    But then how do I trigger a code to Close the Workbook if the user went MIA and Session expired?
    You can use the Session_End event in global.asax to execute code when a session ends. It only works for InProc sessions (the default kind).

     I'll look into how to save the object in Session variable

    // saving to session variable
    Session["workbook"] = workBook;
    
    //retrieving from Session variable
    var workBook = (Excel.WorkBook)Session["workbook"];




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2015 4:50 AM
  • User-1143177497 posted

    Thanks, that helped a lot

    However I can't seem to find the Session_End event, it's not in my global.asax

    And where would I define the Excel object so that it is also accessible by Session_End event ?

    globalasax

    Monday, July 6, 2015 6:11 AM
  • User-821857111 posted

    You can add the event handler to global.asax:

    Sub Session_End(sender As Object, e As EventArgs)
    
    End Sub

    You add a workbook to a session variable wherever you need to (usually in your code behind). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 6, 2015 10:34 AM
  • User753101303 posted

    Hi,

    Or let them to update all parameters and generate the worksheet once for all?  I'm not sure to get why you need to keep the Excel workbook opened.

    Also:
    - using server side automation is not recommended. Excel was not done to be automated server side. http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK or a 3rd party library could help
    - make sure to understand what happens client side and server side. Sometimes new devs are caugth when realizing that what they have done on their dev box can't and won't work with a real web server (it seemed ok just because the same machine was used as both the browser client and the web server).

    How does your page opens the Workbook? You CAN'T do that from server side code (on a real web server at best it would open Excel ON THE SERVER). I'm afraid you are currently doing something like that.

    Thursday, July 9, 2015 12:40 PM