using static objects (declared in global.asax) in webforms

  • Thread starter Thread starter Faisal
  • Start date Start date
F

Faisal

Hi.

I'm in the process of moving an application from ASP to ASP.NET, & I'm
writing in VB, using VS.NET. I'm new to the .NET framework & have a
basic question regarding static objects defined in global.asax.

In the global.asax file I want to declare some static objects (like an
ADODB.Connection, an ADODB.Recordset, a
Scripting.FileSystemObject...), and so I've done so using object tags.
For example I have:
<object runat=server id=DBConn progid="ADODB.Connection
scope=session />

So this should add DBConn to the StaticObjects collection in
HttpApplicationState, and should add DBConn to the namespace of all
the other aspx pages in the application. My problem, though, is when
I refer to any of these static objects in later pages I get build
errors. For example in the code behind page, WebForm1.aspx.vb, if I
write a statement like adcn.Open(...), I get the error: Name 'adcn' is
not declared. Am I making a mistake somewhere here? Is there
something wrong with my syntax? Or do I need to import any specific
files (so far WebForm1.aspx.vb imports ADODB, Scripting, System,
System.Web, & System.Web.UI.HtmlControls)?

I've tried instead doing things like: Application("adcn"), but I don't
think I should need to do this as adcn should already be present in
the namespace. I've alternatively tried to declare the variables
inside the sub Appliation_Start instead of using <object> tags.
However, then I try that like Glboal.adcn.open(...) I get errors
saying something like I must create an object reference first.

I think this must be a trivial question, but I can't seem to find the
answer in my searches online. Any help would be appreciated.

Thanks in advance,
Faisal
 
Sorry for the double post, but I noticed some confusing
typos in my first post (sorry). So here is a typo-free
version of my question:
--------------
Hi.

I'm in the process of moving an application from ASP to
ASP.NET, & I'm writing in VB, using VS.NET. I'm new to
the .NET framework & have a basic question regarding
static objects defined in global.asax.

In the global.asax file I want to declare some static
objects (like an ADODB.Connection, an ADODB.Recordset, a
Scripting.FileSystemObject...), and so I've done so using
object tags. For example I have:
<object runat=server id=DBConn
progid="ADODB.Connection scope=application />

So this should add DBConn to the StaticObjects collection
in HttpApplicationState, and should add DBConn to the
namespace of all the other aspx pages in the application.
My problem, though, is when I refer to any of these static
objects in later pages I get build errors. For example in
the code behind page, WebForm1.aspx.vb, if I write a
statement like DBConn.Open(...), I get the error:
Name 'DBConn' is not declared. Am I making a mistake
somewhere here? Is there something wrong with my syntax?
Or do I need to import any specific files (so far
WebForm1.aspx.vb imports ADODB, Scripting, System,
System.Web, & System.Web.UI.HtmlControls)?

I've tried instead doing things like: Application
("DBConn"), but I don't think I should need to do this as
adcn should already be present in the namespace. I've
alternatively tried to declare the variables inside the
sub Appliation_Start instead of using <object> tags.
However, then I try that like Global.DBConn.open(...) I
get errors saying something like I must create an object
reference first.

I think this must be a trivial question, but I can't seem
to find the answer in my searches online. Any help would
be appreciated.

Thanks in advance,
Faisal
 
First of all, you shouldn't be using ADODB. Use the appropriate managed
classes instead, from the System.Data namespace. Secondly, caching
Connections has NEVER been a good idea, and is especially so with ASP.Net,
as the .Net Framework fully leverages Connection Pooling, and ADO.Net
doesn't work much like ADODB internally.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Hi Kevin,

Thanks for your advice; I'm reading up on using ADO.NET
and moving way from ADODB as you suggested. Can you (or
anyone else) suggest some links where I can read up on
connection pooling?

Secondly, since the .NET framework does support ADO, if I
wanted to initially just move my web application from ASP
to ASP.NET by making the minimal amount of changes, do you
see a way I can fix my problem? I recognize that I should
work on implementing classes from System.Data & System.IO
instead of ADODB & Scripting to get the most out of .NET.
However, since I'm new at most of this, and I figure that
an easier strategy for me would be to first work on simply
getting the web application up & running in .NET, and then
go back to make the changes wrt the way I'm doing database
interaction & file I/O.

With that said, I've been trying initially just to move
stuff from global.asa to global.asax, and am still rather
confused why the static objects are not recongized in all
my .aspx.vb files. Any ideas on that?

Many thanks,
-Faisal
 
Back
Top