473,666 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global.asax Variables

Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml
Document in "Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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

May 16 '07 #1
10 7962
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.

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48******** *************** ***********@mic rosoft.com...
Hello,

I was trying to connect to an Xml database, and I thought of loading the
Xml Document in "Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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


May 16 '07 #2
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.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48******** *************** ***********@mic rosoft.com...
Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml Document in
"Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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

May 16 '07 #3
First, I'm confused as to how dbxml = new Xmldb(Server.Ma pPath("~"));
maps to some sort of object?

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

public static Xmldb dbxml ;

void Application_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("whatever "));

}

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

Global.dbxml;

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Yehia A.Salam" wrote:
Hello,

I was trying to connect to an Xml database, and I thought of loading the Xml
Document in "Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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
May 16 '07 #4
"Peter Bromberg [C# MVP]" <pb*******@yaho o.yabbadabbadoo .comwrote in
message news:41******** *************** ***********@mic rosoft.com...
First, I'm confused as to how dbxml = new Xmldb(Server.Ma pPath("~"));
maps to some sort of object?

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

public static Xmldb dbxml ;

void Application_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("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.
--
John Saunders [MVP]
May 16 '07 #5
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.SelectUse rNode()...

"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:ur******** ******@TK2MSFTN GP04.phx.gbl...
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.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48******** *************** ***********@mic rosoft.com...
>Hello,

I was trying to connect to an Xml database, and I thought of loading the
Xml Document in "Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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

May 17 '07 #6
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_St art 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.
"Peter Bromberg [C# MVP]" <pb*******@yaho o.yabbadabbadoo .comwrote in
message news:41******** *************** ***********@mic rosoft.com...
First, I'm confused as to how dbxml = new Xmldb(Server.Ma pPath("~"));
maps to some sort of object?

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

public static Xmldb dbxml ;

void Application_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("whatever "));

}

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

Global.dbxml;

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Yehia A.Salam" wrote:
>Hello,

I was trying to connect to an Xml database, and I thought of loading the
Xml
Document in "Application_St art" 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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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
May 17 '07 #7
re:
!Xmldb is a class located in the App_code folder used to
!communicate with the xml database ie: dbxml.SelectUse rNode()...

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="Your ClassName" %>
....at the top of your C# page.

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


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:9B******** *************** ***********@mic rosoft.com...
>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.SelectUs erNode()...
"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:ur******** ******@TK2MSFTN GP04.phx.gbl...
>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.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============== =============== =========
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48******* *************** ************@mi crosoft.com...
>>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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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



May 17 '07 #8
but where is csc.exe? I have visual studio 2005

"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
re:
!Xmldb is a class located in the App_code folder used to
!communicate with the xml database ie: dbxml.SelectUse rNode()...

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="Your ClassName" %>
...at the top of your C# page.

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


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:9B******** *************** ***********@mic rosoft.com...
>>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.SelectU serNode()...
>"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:ur******* *******@TK2MSFT NGP04.phx.gbl.. .
>>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.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============= =============== ==========
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48****** *************** *************@m icrosoft.com...
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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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



May 18 '07 #9
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.

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:C8******** *************** ***********@mic rosoft.com...
but where is csc.exe? I have visual studio 2005

"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:u$******** ******@TK2MSFTN GP04.phx.gbl...
>re:
!Xmldb is a class located in the App_code folder used to
!communicate with the xml database ie: dbxml.SelectUse rNode()...

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="Your ClassName" %>
...at the top of your C# page.

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


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============== =============== =========

"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:9B******* *************** ************@mi crosoft.com...
>>>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.Select UserNode()...
>>"Juan T. Llibre" <no***********@ nowhere.comwrot e in message
news:ur****** ********@TK2MSF TNGP04.phx.gbl. ..
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.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============ =============== ===========
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:48***** *************** **************@ microsoft.com.. .
Hello,
>
I was trying to connect to an Xml database, and I thought of loading
the Xml Document in
"Applicatio n_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_Sta rt(object sender, EventArgs e){
dbxml = new Xmldb(Server.Ma pPath("~"));
// 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



May 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
5155
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I wanna use global.asax) --- Would you declare your static var as --- public static int x ;
6
3626
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? Thanks in Advance! Andrea
5
3934
by: WJ | last post by:
I am attempting to use the Global.Asax to store my user's configuration. Here is the concept: 1. User logs on into the site using Form Authentication. 2. I capture the user Credential, verify it and then assign this Logon ID (user) a so called User's serverside cookie. 3. My system is configured to accept 1,024 concurrent users, this means that my Global.Asax will host no more than 1,024 Logon IDs and their associated cookies/variables....
22
3767
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 manipulate them. It starts > and ends with <script></script> tags. > > Yours looks like a compiled version of it.
12
3811
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 (the oleDBConnection on global.asa.vb) at the other aspx pages ?
2
5201
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb file, and what would I do to access it? (I am using VB.NET for my code) Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
8
4859
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 differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web page. I will be grateful if some one can help me understand the difference.
8
13189
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 compiles & runs fine locally, but when I copy to the production machine, I get this error: Parser Error Message: Could not load type 'Global'. Source Error: Line 1: <%@ Application Codebehind="Global.asax.vb" Inherits="Global" %> Source...
15
2556
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 Global class. Note: I use these shared variables for read only values that are set at application start. It would appear the 2.0 doesn't like you to use shared variables in the global class. How do I convert my 1.1 applications to 2.0 without...
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.