Database interactions don't fire from Application_Start in Global.asax?

Discussion in 'ASP .Net' started by Christian Blackburn, Jun 16, 2006.

  1. Hi Gang,

    Let me start by saying I'm using Visual Web Developer 2005 and ASP.net
    2.0. Is there something I have to do to get my Global.asax fire when
    my application loads. If I set a breakpoint nothing happens also I can
    tell that it's not updating my database. I'm storing the users's
    session ID in the database to prevent multiple logins. However, when
    their session expires it's not clearning their record. Are there
    prohibitive constraints on the functionality accessible from
    Global.asax?

    Thanks a bundle,
    Christian Blackburn
    -------------------------------------------------------------------------------------------------------------------------------------

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    'Creates the database's Connection object
    Dim oConn As New System.Data.Odbc.OdbcConnection("DSN=" &
    strDSN)

    'Creates a Command object
    Dim oCommand As New System.Data.Odbc.OdbcCommand

    'Opens the connection
    oConn.Open()

    'Specifies the command to execute,
    'we're clearing all the recorded session IDs
    oCommand.CommandText = "UPDATE users " & vbCrLf & _
    "SET session_id = '';"

    MsgBox(oCommand.CommandText)

    'Tells the command object to use the connection above
    oCommand.Connection = oConn

    'Executes the command
    oCommand.ExecuteNonQuery()

    'Closes the database connection
    oConn.Close()

    End Sub

    -------------------------------------------------------------------------------------------------------------------------------------
    Christian Blackburn, Jun 16, 2006
    #1
    1. Advertising

  2. Hi Gang,

    Well no-one responded and I wound up using some sample code off the
    web. The structure of my file was wrong and so it wasn't being fired.
    If you need a working copy too:

    have a look at the following:
    ------------------------------------------------------------------------------------------------------------------------------
    <%@ Import Namespace="System.Data.ODBC" %>

    <script language="VB" runat="server">

    Const strDSN = "motoadvisor_mysqlConn"

    Sub Application_Start(Sender As Object, E As EventArgs)


    Dim oConn As New OdbcConnection("DSN=" & strDSN)
    Dim oCommand As New OdbcCommand

    oConn.Open()

    oCommand.Connection = oConn

    oCommand.CommandText = "Update users " & _
    "SET session_id = ''"


    oCommand.ExecuteNonQuery()

    End Sub

    Sub Application_End(Sender As Object, E As EventArgs)
    ' Clean up application resources here
    End Sub

    Sub Session_Start(Sender As Object, E As EventArgs)

    'Sets the default timeout period for a session to 20 minutes
    Session.Timeout = 20
    End Sub

    Sub Session_End(Sender As Object, E As EventArgs)
    Dim oConn As New OdbcConnection("DSN=" & strDSN)
    Dim oCommand As New OdbcCommand

    oConn.Open()

    oCommand.Connection = oConn

    oCommand.CommandText = "Update users " & _
    "SET session_id = '' " & _
    "WHERE session_id = '" &
    Session.SessionID & "'"

    oCommand.ExecuteNonQuery()
    End Sub

    Sub Application_Error(Sender As Object, E As EventArgs)

    End Sub

    </script>
    ------------------------------------------------------------------------------------------------------------------------------

    Thanks,
    Christian Blackburn
    Christian Blackburn, Jun 16, 2006
    #2
    1. Advertising

  3. Please review this article which details the Application lifecycle :

    http://msdn2.microsoft.com/en-us/library/ms178473.aspx

    Application_Start fires *once* in the application's life.
    You cannot use it to capture data about individual users.

    Try putting your code in the Session_Start event handler in global.asax.

    btw, you'll need to modify your code so that

    1. it actually inserts the session id into your database
    2. It doesn't use MsgBox ( Msgbox runs client-side, not server-side )




    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Christian Blackburn" <> wrote in message
    news:...
    > Hi Gang,
    >
    > Let me start by saying I'm using Visual Web Developer 2005 and ASP.net
    > 2.0. Is there something I have to do to get my Global.asax fire when
    > my application loads. If I set a breakpoint nothing happens also I can
    > tell that it's not updating my database. I'm storing the users's
    > session ID in the database to prevent multiple logins. However, when
    > their session expires it's not clearning their record. Are there
    > prohibitive constraints on the functionality accessible from
    > Global.asax?
    >
    > Thanks a bundle,
    > Christian Blackburn
    > -------------------------------------------------------------------------------------------------------------------------------------
    >
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >
    > 'Creates the database's Connection object
    > Dim oConn As New System.Data.Odbc.OdbcConnection("DSN=" &
    > strDSN)
    >
    > 'Creates a Command object
    > Dim oCommand As New System.Data.Odbc.OdbcCommand
    >
    > 'Opens the connection
    > oConn.Open()
    >
    > 'Specifies the command to execute,
    > 'we're clearing all the recorded session IDs
    > oCommand.CommandText = "UPDATE users " & vbCrLf & _
    > "SET session_id = '';"
    >
    > MsgBox(oCommand.CommandText)
    >
    > 'Tells the command object to use the connection above
    > oCommand.Connection = oConn
    >
    > 'Executes the command
    > oCommand.ExecuteNonQuery()
    >
    > 'Closes the database connection
    > oConn.Close()
    >
    > End Sub
    >
    > -------------------------------------------------------------------------------------------------------------------------------------
    >
    Juan T. Llibre, Jun 16, 2006
    #3
  4. Christian Blackburn

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:...

    > 2. Msgbox runs client-side, not server-side


    Having a bit of a senior moment, Juan...? :)
    Mark Rae, Jun 16, 2006
    #4
  5. re:
    >> 2. Msgbox runs client-side, not server-side

    > Having a bit of a senior moment, Juan...? :)


    Not really.

    They way he coded it, in Application_Start,
    MsgBox would attempt to run on the server:

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ....snip...
    MsgBox(oCommand.CommandText)
    ....snip...

    That will throw an error. See : http://p2p.wrox.com/topic.asp?TOPIC_ID=9675

    Am I missing something ?

    For a nifty MsgBox server control for ASP.NET, see :
    http://www.codeproject.com/aspnet/NingLiangSimpleControl.asp



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Mark Rae" <> wrote in message
    news:...
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >
    >> 2. Msgbox runs client-side, not server-side

    >
    > Having a bit of a senior moment, Juan...? :)
    Juan T. Llibre, Jun 16, 2006
    #5
  6. Christian Blackburn

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:...

    >>> 2. Msgbox runs client-side, not server-side

    >> Having a bit of a senior moment, Juan...? :)

    >
    > Not really.
    >
    > They way he coded it, in Application_Start,
    > MsgBox would attempt to run on the server:


    Indeed - that's why I was puzzled that you said Msgbox runs client-side, not
    server-side...
    Mark Rae, Jun 16, 2006
    #6
  7. re:
    >> They way he coded it, in Application_Start,
    >> MsgBox would attempt to run on the server:

    > that's why I was puzzled that you said Msgbox runs client-side, not server-side...


    It *does* run client-side. In fact, it can *only* run on the client.

    Juan wrote :
    > you'll need to modify your code so that

    ....snip...
    > it doesn't use MsgBox ( Msgbox runs client-side, not server-side )


    IOW, he was coding it thinking that MsgBox would run client-side,
    if he coded it into Application_Start. It won't run from Application_Start.

    They way he coded it, MsgBox would attempt to run on the server, which it can't do.

    Maybe it's just a semantics thing... ;-)



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Mark Rae" <> wrote in message
    news:...
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >
    >>>> 2. Msgbox runs client-side, not server-side
    >>> Having a bit of a senior moment, Juan...? :)

    >>
    >> Not really.
    >>
    >> They way he coded it, in Application_Start,
    >> MsgBox would attempt to run on the server:

    >
    > Indeed - that's why I was puzzled that you said Msgbox runs client-side, not server-side...
    >
    Juan T. Llibre, Jun 16, 2006
    #7
  8. Christian Blackburn

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:Ol$...

    > Maybe it's just a semantics thing... ;-)


    Yeah, sorry - I was pretty sure I knew what you meant... :)
    Mark Rae, Jun 16, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Halcyon Woodward
    Replies:
    3
    Views:
    992
    Halcyon Woodward
    Jul 16, 2003
  2. Max
    Replies:
    5
    Views:
    20,840
    liliancho
    Dec 7, 2011
  3. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    797
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  4. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,136
    Mark Rae
    May 23, 2006
  5. Replies:
    4
    Views:
    833
    Juan T. Llibre
    Oct 19, 2007
Loading...

Share This Page