473,396 Members | 2,009 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,396 software developers and data experts.

Global.asax Variables

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

May 16 '07 #1
10 7949
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*****@hotmail.comwrote in message
news:48**********************************@microsof t.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_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


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*****@hotmail.comwrote in message
news:48**********************************@microsof t.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_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

May 16 '07 #3
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;

--
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_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
May 16 '07 #4
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:41**********************************@microsof t.com...
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.
--
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.SelectUserNode()...

"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ur**************@TK2MSFTNGP04.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*****@hotmail.comwrote in message
news:48**********************************@microsof t.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_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

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_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.
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:41**********************************@microsof t.com...
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;

--
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_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
May 17 '07 #7
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.


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*****@hotmail.comwrote in message
news:9B**********************************@microsof t.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.SelectUserNode()...
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ur**************@TK2MSFTNGP04.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*****@hotmail.comwrote in message
news:48**********************************@microso ft.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_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



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

"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:u$**************@TK2MSFTNGP04.phx.gbl...
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.


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*****@hotmail.comwrote in message
news:9B**********************************@microsof t.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.SelectUserNode()...
>"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ur**************@TK2MSFTNGP04.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*****@hotmail.comwrote in message
news:48**********************************@micros oft.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_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



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*****@hotmail.comwrote in message
news:C8**********************************@microsof t.com...
but where is csc.exe? I have visual studio 2005

"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:u$**************@TK2MSFTNGP04.phx.gbl...
>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.


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*****@hotmail.comwrote in message
news:9B**********************************@microso ft.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.SelectUserNode()...
>>"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ur**************@TK2MSFTNGP04.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*****@hotmail.comwrote in message
news:48**********************************@micro soft.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_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



May 18 '07 #10
csc.exe is the C# compiler.

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


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*****@hotmail.comwrote in message
news:C8**********************************@microsof t.com...
but where is csc.exe? I have visual studio 2005

"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:u$**************@TK2MSFTNGP04.phx.gbl...
>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.


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*****@hotmail.comwrote in message
news:9B**********************************@microso ft.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.SelectUserNode()...
>>"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ur**************@TK2MSFTNGP04.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*****@hotmail.comwrote in message
news:48**********************************@micro soft.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_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




May 18 '07 #11

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

Similar topics

25
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...
6
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? ...
5
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...
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...
2
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...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
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...

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.