Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Database interactions don't fire from Application_Start in Global.asax?

Reply
Thread Tools

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

 
 
Christian Blackburn
Guest
Posts: n/a
 
      06-16-2006
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

-------------------------------------------------------------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Christian Blackburn
Guest
Posts: n/a
 
      06-16-2006
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

 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-16-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>
> -------------------------------------------------------------------------------------------------------------------------------------
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      06-16-2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


Having a bit of a senior moment, Juan...?


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-16-2006
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/Ni...pleControl.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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 2. Msgbox runs client-side, not server-side

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



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      06-16-2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      06-16-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>>> 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...
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      06-16-2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:Ol$(E-Mail Removed)...

> Maybe it's just a semantics thing...


Yeah, sorry - I was pretty sure I knew what you meant...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application_Start in Global.asax doesnt fire! gamesforums@hotmail.com ASP .Net 4 10-19-2007 02:04 PM
Application_Start Event does not fire Steve ASP .Net 1 05-03-2006 06:19 PM
URGENT: Application_Start - doesn't fire Gordon Smith ASP .Net 7 02-28-2005 06:41 PM
Why does Application_Start fire twice? Rashad Rivera ASP .Net 5 05-01-2004 02:37 AM
HttpApplication Init, Application_Start and Application_End don't fire Halcyon Woodward ASP .Net 3 07-16-2003 04:34 PM



Advertisments