how to use global.asax?

C

Chris

Hi,

I want to put the namespaces and the data connection into global.asax like
this:
<%@ import namespace="System.Data"%>
<%@ import namespace="System.Data.OleDb"%>
<script runat="server">
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim oConnection As System.Data.OleDb.OleDbConnection
oConnection = New System.Data.OleDb.OleDbConnection()
Dim comd As System.Data.OleDb.OleDbCommand
Dim dtreader As System.Data.OleDb.OleDbDataReader
Dim sConnectionString As String
sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
= c:\mydb.mdb"
oConnection.ConnectionString = sConnectionString
oConnection.Open()
End Sub
</script>

But how to link the global.asax file to the file (fetch.aspx) which must
fetch the data?
I did this in fetch.aspx file which generates an error "Name 'comd' is not
declared"

comd = New System.Data.OleDb.OleDbCommand("select count(*) from pc",
oConnection)

Thanks for helping.
Chris
 
M

Mark Rae

This is one of the worst thing you can do in ASP.NET - the absolute LAST
thing you should do is to keep a connection to your RDBMS open for any
longer than is necessary, let alone for the entire lifetime of your web app.
Consider using a DAL (Data Access Layer) instead:
http://www.15seconds.com/issue/030317.htm

Also, be aware that using MS Access will severely restrict the number of
concurrent users that your web app can support - you should seriously
consider using a server RDBMS instead, preferably one with a native .NET
data provider e.g. SQL Server, MSDE / SQL Server Express, MySql etc...
 
P

Patrice

global.asax works by itself (assuming the application root is defined as a
web application in the IIS console). You don't have to link it with a
particular page. Its purpose is to define events that will be triggered when
appropriate (ie. your code will be automatically triggered when the
application starts).

As a side note :
- you are declaring a local variable. It will be seen only in this event.
- keeping a connection around is considered as a bad practice.

The preferred way is to create the connection when needed (behing the scene
the connection is returned from a pool). This way each request use its own
connection. You can serve a number of users with fewer connections (if you
have 100 users, you'll still have currently 10 open connections if 10 users
are actually running something and others are just reading the web page they
just get).
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

You have declared the reference for the command in the method. That
means that it's a local variable, and when it goes out of scope when the
methods ends, it's lost forever.

Creating a database connection in Application_Start makes no sense
anyway, unless it's only used inside the method and properly closed. If
you would try to use the connection in the web pages, you would quickly
run into problems, as every user that requests a page will be sharing
the same connection. That means that only one user at a time can request
a page, or you will get an error message because the connection is busy.
Every page that uses the database needs it's own connection.
 
C

Chris

Ok, thanks for your advices ..;


Göran Andersson said:
You have declared the reference for the command in the method. That
means that it's a local variable, and when it goes out of scope when the
methods ends, it's lost forever.

Creating a database connection in Application_Start makes no sense
anyway, unless it's only used inside the method and properly closed. If
you would try to use the connection in the web pages, you would quickly
run into problems, as every user that requests a page will be sharing
the same connection. That means that only one user at a time can request
a page, or you will get an error message because the connection is busy.
Every page that uses the database needs it's own connection.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top