473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to use global.asax?

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
4 2352
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
12
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
5
by: ad | last post by:
The Global.asax is code-inside with default. How to change Global.asax to code-behind?
2
by: Steve | last post by:
I am new to this newsgroup & to .NET in general. I have been playing around with Visual Studio .NET, building and rendering web pages using VB "code behind" files. My problem / question is; How...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
4
by: Al Santino | last post by:
Hello, I've created a simple C# web services project using Visual Studio 2005. My service compiles and runs correctly when called by remote clients. I'm able to step through the service in the...
16
by: thefritz_j | last post by:
We just converted our VS2003 1.1 VB web project (which was working fine) to VS2005 2.0 and now I get: Parser Error Message: Could not load type '<Namespace>.'. Source Error: Line 1: <%@...
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.