Regarding Session_End and Application_End methods in asp.net

3.2k Views Asked by At

I'm trying to implement a class (lets call it CustomerConnection class) which implements System.Web.HttpApplication interface. I tried to implement two events session_end and application_end events in this class in this way:

public class CustomerConnection: System.Web.HttpApplication
{


    protected void Session_End(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        SqlConnection connection = context.Items[Database_Con] as SqlConnection;
        connection.close();

    }

    protected void Application_End(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        //someotherEvent
    }

But it seems these events are not getting executed neither for the application end or session log out.

How should i make these events get´executed ? Is it like i have to notify to certain other event?

I'm just creating a new object of customer class. is it enough to get these events executed or my approach is fundamentally wrong?

Please help me out

Thanks in anticipation

2

There are 2 best solutions below

0
On BEST ANSWER

You need this in global.asax:

<%@ Application Language="C#" Inherits="CustomerConnection"%>

Caveat, I've never tried this, I found it here: http://www.codedigest.com/Articles/ASPNET/34_Adding_CodeBehind_for_Gloabalasaz_x_file_in_aspnet_20.aspx

0
On

The object that you have created isn't connected to anything, so it can't receive any events from the application. You have to put the event handlers in the object that is used for the web application, i.e. in the Global.asax.cs file.

Besides, there is no Session_End event, it's Session_OnEnd.