Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Database interactions don't fire from Application_Start in Global.asax? (http://www.velocityreviews.com/forums/t300320-database-interactions-dont-fire-from-application_start-in-global-asax.html)

Christian Blackburn 06-16-2006 07:26 AM

Database interactions don't fire from Application_Start in Global.asax?
 
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 06-16-2006 01:16 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
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


Juan T. Llibre 06-16-2006 01:33 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
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" <christian.Blackburn@Yahoo.com> wrote in message
news:1150442804.646872.26720@y41g2000cwy.googlegro 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
>
> -------------------------------------------------------------------------------------------------------------------------------------
>




Mark Rae 06-16-2006 02:49 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:uiYNBmUkGHA.1508@TK2MSFTNGP04.phx.gbl...

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


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



Juan T. Llibre 06-16-2006 03:09 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
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" <mark@markN-O-S-P-A-M.co.uk> wrote in message
news:ed9qBQVkGHA.4104@TK2MSFTNGP04.phx.gbl...
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
> news:uiYNBmUkGHA.1508@TK2MSFTNGP04.phx.gbl...
>
>> 2. Msgbox runs client-side, not server-side

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




Mark Rae 06-16-2006 05:19 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:uLQUkbVkGHA.3588@TK2MSFTNGP02.phx.gbl...

>>> 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 06-16-2006 07:08 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
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" <mark@markN-O-S-P-A-M.co.uk> wrote in message
news:OKKRIkWkGHA.1272@TK2MSFTNGP03.phx.gbl...
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
> news:uLQUkbVkGHA.3588@TK2MSFTNGP02.phx.gbl...
>
>>>> 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 06-16-2006 07:40 PM

Re: Database interactions don't fire from Application_Start in Global.asax?
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:Ol$pzgXkGHA.4672@TK2MSFTNGP02.phx.gbl...

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


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




All times are GMT. The time now is 12:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57