By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,602 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,602 IT Pros & Developers. It's quick & easy.

how to use global.asax?

P: n/a
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
May 10 '06 #1
Share this Question
Share on Google+
4 Replies

P: n/a
"Chris" <cc*@sdsd.dc> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

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...
May 10 '06 #2

P: n/a
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).

--
Patrice

"Chris" <cc*@sdsd.dc> a écrit dans le message de news:
%2****************@TK2MSFTNGP03.phx.gbl...
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

May 10 '06 #3

P: n/a
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.

Chris wrote:
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

May 10 '06 #4

P: n/a
Ok, thanks for your advices ..;
"Göran Andersson" <gu***@guffa.com> wrote in message
news:Ow**************@TK2MSFTNGP02.phx.gbl...
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.

Chris wrote:
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

May 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.