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

  • Thread starter Christian Blackburn
  • Start date
C

Christian Blackburn

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

-------------------------------------------------------------------------------------------------------------------------------------
 
C

Christian Blackburn

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>
 
J

Juan T. Llibre

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 )
 
J

Juan T. Llibre

re:
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
 
M

Mark Rae

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...
 
J

Juan T. Llibre

re:
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... ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top