ASP.Net: global.asax generic error handling - Can it close an open file?

Posted on 2012-08-30
Last Modified: 2012-09-03
On our site, we use global.asax's Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) to catch pretty much all errors thrown. Right now, this sub sends an email with error details. We wish it to stay that way, that is, we don't wish to start using lots of try/catch blocks. Errors, when they occur, have to be studied and fixed so they don't happen again.

However, there are a couple pages where the user can upload a text file, which is then imported into the DB. When an error occurs, the uploaded file becomes locked and can't be moved/deleted until IIS decides to release it (not sure how long that takes, but it's too long).

We can resume the procedure in 3 steps:
1- Open the file
2- Read the file and use those values
3- Close the file

When an error occurs on step 2, the file is then locked. So, is there any way for global.asax's sub to close the file?
Question by:Cluskitt
    LVL 83

    Accepted Solution

    Not unless you dispose the reader which has opened the file. I think you may need to use try catch in this instance. What you can do is rethrow the exception so your global code still works

       'code to process file
    Catch Ex As Exception
       'code to release file
       Throw New Exception("Rethrowing", ex)
    End Try
    LVL 18

    Author Comment

    I was mostly wandering if there was a way to send the reader as a parameter. Or, if it would be feasible to set it as a global object (which would allow global.asax to close it)
    LVL 83

    Expert Comment

    Parameter would not be possible I think. Global variable is possible but I would prefer the technique shown above.
    LVL 18

    Author Closing Comment


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now