Global.asax Variables

Y

Yehia A.Salam

Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml
Document in "Application_Start" so that the xml is loaded only once and then
queried later as many times as requested, this should the efficient way of
connecting to the database afaik, however I can't access the xml object in
my aspx files with this error "dbxml does not exist in current context":

<%@ Application Language="C#" %>
<script runat="server">
Xmldb dbxml;
void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("~"));
// Code that runs on application startup

}...

I though that any variable declared in the global.asax files should be
available to any other aspx page, how could I access the variable?

Thanks In Advance
Yehia A.Salam
 
A

Aidy

Variables you define in the global.asax events are still only local to that
function and are out of scope when the function ends. IE although the file
is called global.asax, the vars in it are not actually global.

Use static properties on a configuration class or google "singleton pattern"
for more info on how to do this.
 
J

Juan T. Llibre

What is Xmldb ?

Is it an object recognized by the .Net Framework ?
Have you imported/declared it ?

"dbxml does not exist in current context" is a fairly clear error message.

The .Net Framework is telling you that it doesn't recognize Xmldb.
That's why you're not able to create dbxml as an instance of Xmldb.
 
G

Guest

First, I'm confused as to how dbxml = new Xmldb(Server.MapPath("~"));
maps to some sort of object?

But assuming that you get that figured out, you could try:

public static Xmldb dbxml ;

void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("whatever"));

}

At this point, you should be able to gain access from any page with:

Global.dbxml;
 
J

John Saunders [MVP]

Peter Bromberg said:
First, I'm confused as to how dbxml = new Xmldb(Server.MapPath("~"));
maps to some sort of object?

But assuming that you get that figured out, you could try:

public static Xmldb dbxml ;

void Application_Start(object sender, EventArgs e){
dbxml = new Xmldb(Server.MapPath("whatever"));

}

At this point, you should be able to gain access from any page with:

Global.dbxml;

Please note - this only works in cases where you will never modify the data,
and it only works in cases where only one thread can set the variable.
Otherwise, you need some form of locking to prevent simultaneous
modification by several threads.

Note that ASP.NET page requests are handled on worker threads, so if you
have multiple requests accessing this static data, you have the potential
for problems if more than one can update it at a time.

I know the OP was talking about reading the data only at startup time, but I
emphasize the above for anyone else reading this who didn't pick up on that
context.
 
Y

Yehia A.Salam

I know how clear the error message is, I was asking for a workaround for
this problem, Xmldb is a class located in the App_code folder used to
communicate with the xml database ie: dbxml.SelectUserNode()...
 
Y

Yehia A.Salam

I tried it but Global is not recognized in any other page, am I on the right
track, should I open the connection on the Appplication_Start event and
leave it the whole lifetime of the application, or should I used the
Session_start event (considering my xml file is relatively small < 5mb), is
Global.asax where the database connection should be instantiated, this
should be a very common task to asp.net developers however I didn't found
any useful resources on the net that could guide me on how to do the
connection.
 
J

Juan T. Llibre

re:
!> Xmldb is a class located in the App_code folder used to
!> communicate with the xml database ie: dbxml.SelectUserNode()...

Your problem is that at the time the application starts,
the code in App_Code hasn't been compiled yet.

What I would suggest is pre-compiling your Xmldb class, from the command-line,
and placing the resulting assembly in the /bin directory of your application.

csc /t:library /out:Xmldb.dll Xmldb.cs

If you need to import .Net classes, include them in your command line:
csc /t:library /r:system.data /r:system.xml /out:Xmldb.dll Xmldb.cs

After you compile the assembly, you can instantiate the class,
so you're able to call its methods in your .aspx pages with :

<%@ Import Namespace="YourClassName" %>
....at the top of your C# page.

Now, you can use code in any page to access the
dbxml.SelectUserNode() method in your assembly.
 
Y

Yehia A.Salam

but where is csc.exe? I have visual studio 2005

Juan T. Llibre said:
re:
!> Xmldb is a class located in the App_code folder used to
!> communicate with the xml database ie: dbxml.SelectUserNode()...

Your problem is that at the time the application starts,
the code in App_Code hasn't been compiled yet.

What I would suggest is pre-compiling your Xmldb class, from the
command-line,
and placing the resulting assembly in the /bin directory of your
application.

csc /t:library /out:Xmldb.dll Xmldb.cs

If you need to import .Net classes, include them in your command line:
csc /t:library /r:system.data /r:system.xml /out:Xmldb.dll Xmldb.cs

After you compile the assembly, you can instantiate the class,
so you're able to call its methods in your .aspx pages with :

<%@ Import Namespace="YourClassName" %>
...at the top of your C# page.

Now, you can use code in any page to access the
dbxml.SelectUserNode() method in your assembly.
 
Y

Yehia A.Salam

ok, I created a new class project and added the reference to my website, I
can access the dll normally now, however I still don't have a global
variable to access the instance of Xmldb, I mean I have to instantiate a new
variable in each page to be able to use it:
Xmldb x = new Xmldb();
x.SelectNodes("...");

is there a way to declare x to be global across all pages, so in any page I
could just type x.SelectNodes("") without creating a new instance of Xmldb
each time.
 
J

Juan T. Llibre

csc.exe is the C# compiler.

It's in the .Net Framework 2.0 directory:
Drive:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
 

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,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top