Trying to figure out Global.asax

MasterGinyu

Newcomer
Joined
Mar 11, 2004
Messages
20
Hi, I am wanting to do something fairly simple but it doesn't seem to be working correctly. I have read that you can use the Global.asax to run when certain events are fired. In this specific instance, I am wanting to write to the database the Date and Time when the Session_End event is fired. I have created the Global.asax and put it with my application but it doesn't seem to be writing to the dbase. Let me know if this is the way to do this or if there would be a better way to determine the end of the session.
 
Code:
Imports System.Web
Imports System.Web.SessionState
Imports System.Data
Imports System.Date.OleDb

Public Class Global
    Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#End Region

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when the session is started
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        Dim cnAccess as New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\streech.mdb")
		cnAccess.Open()
		Dim sql,sessionEnd as String
		sessionEnd = DateTime.Now
		sql = "Insert into client_log (session_end) values (" & chr(34) & sessionEnd & chr(34) & ") where (session_id = kyqkdoiicjcldd33f0cwtgyv)"
		Dim cmdInsert as New OleDbCommand(sql,cnAccess)
		cmdInsert.ExecuteNonQuery()
    End Sub

End Class
 
Do you have access to the event viewer? See if you there are any application errors. It might clue you in on if there is an exception thrown from the sub.
 
kahlua001 said:
Do you have access to the event viewer? See if you there are any application errors. It might clue you in on if there is an exception thrown from the sub.

I am running off my local server so I am sure I do have access to it, but I have no clue how to access it.
 
Go to your control panel, check the event viewer. See if there are any errors, and if so, what are the details. Or, take the db insert code and put it in a regular web form, run it and see if there are any errors from that code, or if it is doing what you want it to.
 
Visual Basic:
sql = "Insert into client_log (session_end) values (" & chr(34) & sessionEnd & chr(34) & ") where (session_id = kyqkdoiicjcldd33f0cwtgyv)"

Okay... I know exactly where is your problem. Here is the corrected version :

Visual Basic:
sql = "Insert into client_log (session_end) values ([b]'[/b]" & chr(34) & sessionEnd & chr(34) & "[b]'[/b]) where (session_id = kyqkdoiicjcldd33f0cwtgyv)"
You MUST have ' because SQL don't detect if it is or not a string. It take it as a variable and it's not found. Try it like this and you'll like it.
 
Back
Top