ASP.net Subroutines - how is it called?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a subroutine called Sub Application_BeginRequest. It is in my
Global.asax.vb file. It seems to be called when the application is
launched, but I am not sure how. i thought somewhere in code you have to
actually use the Application_BeginRequest() parameter to invoke the
subroutine. Or is this some built in thing that if you name something
Application_beginRequest, the engine knows to use this sub routine?



Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
gsDataSource = ";Data Source = "
Dim sApplPhysicalPath As String =
Request.ServerVariables("APPL_PHYSICAL_PATH")
Dim sDataRelPath As String = "..\data\data.mdb"

gsDataSource += sApplPhysicalPath & sDataRelPath

Dim sServerName As String =
LCase(Trim(Request.ServerVariables("SERVER_NAME")))
' This line use to read If sServerName = "www.pointe.com" Then
If sServerName = "graddb.bgt.matown.edu/bt/dev" Then
'Dim sPathInfo As String =
LCase(Trim(Request.ServerVariables("PATH_INFO")))
'Dim sDir As String = Left(sPathInfo, InStrRev(sPathInfo, "/"))
'If sDir = "/bt/dev/" Then
gbDevelopment = True
'gsDataSource += sApplPhysicalPath & "..\" & sDataRelPath
' removed an end if statement
Else
' This use to read gbDevelopment = True
gbDevelopment = False
End If
'These lines use to read
'If gbSQLServer Then
'sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\w\btmsm\bt.mdb"
'gsConnectionString = "Provider=SQLOLEDB;Data Source=ccm4;Initial
Catalog=BTMSM;Integrated Security=SSPI"
'Else
'If gbDevelopment Then
'gsConnectionString = OLEDB_PROVIDER & "User
ID=gtmbatestdb;Password=" & ADMIN_PASSWORD & ";Data Source=point"
'Else
'gsConnectionString = OLEDB_PROVIDER & "User ID=ctmsm;Password=" &
ADMIN_PASSWORD & ";Data Source=point"
'End If

' For Production, use different password and use mprod.cgt.bt.buzz
' For Development use mdevl.mgt.gt.buzz

If gbDevelopment Then
gsConnectionString = OLEDB_PROVIDER & "User ID=ctmba;Password="
& DB_PASSWORD & ";Data Source=mdevl.cgt.bt.buzz"
Else
' The end of this string use to read DB_PASSWORD and
mdevl.cgt.bt.buzz
gsConnectionString = OLEDB_PROVIDER & "User ID=ctmba;Password="
& DB_PASSWORD1 & ";Data Source=mprod.cgt.bt.buzz"
End If

End Sub
 
It's called automatically whenever an application is loaded into the
pool as far as I know. This is called only once when loaded, but the
application may be loaded several times (such as when you change the
web.config, or upload a new build of the application, or if the pool
recycles).

HTH,
Darren Kopp
http://blog.secudocs.com/
 
The methods within the global.asax.vb are handlers to events triggered by the
HttpApplication class when a request is made to an ASP.NET web application.

You would find more details in this article "ASP.NET Application Life Cycle
Overview"
http://msdn2.microsoft.com/en-us/library/ms178473.aspx

BTW, I am glad you reached a solution for that LDAP query issue you had a
while ago.
 
This is just an event as in a windows forms application (that is code that
is called by the system not that you call explicitely).
This executed actually for each web server request...

Application_Start is used once when the application starts...

You could perhaps store the connection string somewhere (web.config or as an
application variable).
 
Thanks Phillip, Darren, Patrice, all the responses pointed me in the right
direction. What i was trying to seyup is when aa certain user logs into the
application, i wanted a different set of databse credential accounts to be
used as the OLE DB connection(gsconnection string). I was trying to figure
out where best to put that logic...

Thanks.....
 
You might also consider an alternative approach. The code that you execute
in this routine seems to be concerned only about whether the application is
running in development or production. If that is the case you can achieve
that goal using a less costly solution (from a maintenance perspective; you
do not need to recompile the application if your development application
folder or user ID changed). This can be done using the web.config:

1- add an AppSettings section in your web.config:
<appSettings file="prod.config">
<add key="DBConnectionString" value="user id=MyUserID;data
source=MySchema;password=MyPassword;Connection Lifetime=3600" />
</appSettings>

2- In the application folder in production, create a file named prod.config
that contains only the appSettings section:
<appSettings>
<add key="DBConnectionString" value="user id=ProdUserID;data
source=ProdSchema;password=ProdUserPwd;Connection Lifetime=3600" />
</appSettings>

This syntax instructs the ASP.NET engine to overrides the settings specified
in the <appSettings> element of the web.config with the settings in the file
named prod.config.

For more details on the <appSettings> Element:
http://msdn.microsoft.com/library/d...-us/cpgenref/html/gngrfappsettingselement.asp
 
Booker,

Wrap it up in a class in the Business Logic Layer. I agree with
Phillip in putting the strings in the web.config, but I would put both
connection strings in there like so:

<appSettings>
<add key="DevConnString" value="your development connection string
here" />
<add key="ProdConnString" value="your production connection string
here" />
</appSettings>

Then in your business logic layer I would do something like (and
forgive me if my VB.Net Syntax is wrong)

Public Class DBHelper
Public Static Function GetConnectionString() as String
' run statements to determine what connection string to use
' we'll assume you assign to boolean value isProduction

If Not isProduction Then
' not production, use development
return ConfigurationSetting.AppSettings("DevConnString")
Else
return ConfigurationSettings.AppSettings("ProdConnString")
End If
End Function
End Class

The function will return the connection string that is in the
web.config file. Note that where this class is defined, you need to
import System.Configuration. Then when you create your connection do
do something like

Dim OleDbConnection as New
OleDbConnection(DBHelper.GetConnectionString())

That will make sure you are using the right connection string
everytime.

HTH,
Darren Kopp
http://blog.secudocs.com/
 
Ok, i will have to chew on both of those. I am new to the asp.net
environment. I actually inherited this code, and I am learning on the fly.
So with those excellent examples, I would need to find out how to correctly
import that system configuration you mentioned. But I do appreciate the
depth of the sample solution provided... very thorough.
 
I don't remember what it is with VB.Net, i think it's something like
Inherits System.Configuration.

Just look at the top of the code behind and look for the keyword before
System or System.Web, etc.

so create a VB code file? I think that's what it's called... Anything
with a .vb extension. then this

Inherits System
Inherits System.Configuration
Inherits System.Web
... and so on for whatever you need

namespace Whatever ' replace whatever with your namespace
(usually the project name)
' put class code here
end namespace

again i don't know if that's the right vb syntax, hopefully it is.
it's the right idea though. Since you are just starting, here are some
nice sites with good 1.x tutorials.

http://aspnet.4guysfromrolla.com/1.x/
http://samples.gotdotnet.com/quickstart/aspplus/
http://aspalliance.com/articleViewer.aspx?aId=144&pId=
http://aspalliance.com/articles/LearnVBNET.aspx
http://aspalliance.com/articles/LearnASPNET.aspx

-Darren
 

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

Back
Top