473,320 Members | 1,695 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.

global.asax and application objects?

Hello.

Apologies if this is the wrong group. I have created a class called
"page" which i would like to add to the global.asax and then call from
any control within the website. I have currently got:

global.asax

<%@ import Namespace="myNamespace" %>
<script language="C#" runat="server">
public page webpage;

void Session_Start(Object sender, EventArgs E) {
Response.Write("Session is Starting...<br>");
webpage = new page();
}
</script>

I then want to be able to call the "webpage" object in a control like
so:

webpage.methodName();

However i get a "type or namespace cannot be found" error. Can anyone
help me fix this or perhaps suggest a better method of persisting a
global object throughout the application - i am trying to avoid having
"page webpage = new page();" in each control.

Thanks,

marc

Nov 19 '05 #1
11 4941
Hi Marc,

First, C# is case-sensitive, so unless you have defined a class called
"page" you would certainly get this error.
However i get a "type or namespace cannot be found" error.
Second, you have not created an instance of the class, just a variable for
containing an instance of the class.

Third, while the Global.asax file is compiled at run-time and instantiated
as a class, its purpose is not to be used as a business class. Business
classes should be defined separately. The purpose of the global.asax file is
to define event hsndlers for Application-level events, such as
Application_OnStart, Application_OnEnd, Session_OnStart and Session_OnEnd.
If you want to store an instance of a clsss in Application Scope, your best
bet is to create the instance in one of these event handlers and add it to
the Application Cache.

Fourth, a System.Web.UI.Page class is specifically designed as an
HttpHandler that handles and responds to HTTP Requests. It is not designed
as a business class, and should not be used as one. I can't imagine why
anyone would want to, but I can only guess that you're guessing something
wrong (in addition to what I've already pointed out).

If you can tell us what sort of functionality you're trying to achieve, we
can provide suggestions as to the best way to achieve it. In the meantime, I
would suggest downloading the free Microsoft .Net SDK, and reading up on
ASP.Net:

http://www.microsoft.com/downloads/d...displaylang=en

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Marc" <ma**********@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com... Hello.

Apologies if this is the wrong group. I have created a class called
"page" which i would like to add to the global.asax and then call from
any control within the website. I have currently got:

global.asax

<%@ import Namespace="myNamespace" %>
<script language="C#" runat="server">
public page webpage;

void Session_Start(Object sender, EventArgs E) {
Response.Write("Session is Starting...<br>");
webpage = new page();
}
</script>

I then want to be able to call the "webpage" object in a control like
so:

webpage.methodName();

However i get a "type or namespace cannot be found" error. Can anyone
help me fix this or perhaps suggest a better method of persisting a
global object throughout the application - i am trying to avoid having
"page webpage = new page();" in each control.

Thanks,

marc

Nov 19 '05 #2
Hello.

Thanks for the quick reply. I have created a Namespace.page class which
includes methods for interacting with a database and returning content
for pages and menu, graphic etc. Previously in my controls i would have
said:

public page webpage;
private void page_load(object etc etc etc)
{
page = new webpage(1);
lblHeading = page.getHeading();
lblBody = page.getBody();
dgMenu.DataSource = page.getMenuItems();
dgMenu.DataBind(); (etc etc etc)
}

I want to only create a single instance of this class and call it from
any control so it sounds like:

" If you want to store an instance of a clsss in Application Scope,
your best
bet is to create the instance in one of these event handlers and add it
to
the Application Cache. "

Is what i want to do. I have attempted to do this by:

webpage = new page();
Cache.Insert ("webpage", webpage, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes (15));

in my global.asax.

Now i get a null object reference error - which comes back to what you
said here:

"Second, you have not created an instance of the class, just a variable
for
containing an instance of the class. "

Just i dont understand why it is not an instance of my class? I case of
a little knowledge is dangerous?

Thanks for any help!

ta

marc

Nov 19 '05 #3

- If you want to have an object that's ready for use in each page (tou want
to skip the code that does the initlization) than let the page derive form
a basepage where the object is instantiated.
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx
This object will have a lifetime of that of the page so maybe its not something
you can use.

- If you want to have access to common routines than make a class with static/instance
methods.
This is also a good option for storing data with global scope because you
get intellisense while developing.

Let me know if you have any more questions...

Cheers,
Tom Pester
Hello.

Apologies if this is the wrong group. I have created a class called
"page" which i would like to add to the global.asax and then call from
any control within the website. I have currently got:

global.asax

<%@ import Namespace="myNamespace" %>
<script language="C#" runat="server">
public page webpage;
void Session_Start(Object sender, EventArgs E) {
Response.Write("Session is Starting...<br>");
webpage = new page();
}
</script>
I then want to be able to call the "webpage" object in a control like
so:

webpage.methodName();

However i get a "type or namespace cannot be found" error. Can anyone
help me fix this or perhaps suggest a better method of persisting a
global object throughout the application - i am trying to avoid having
"page webpage = new page();" in each control.

Thanks,

marc

Nov 19 '05 #4
Take a look at my first comment. System.Web.UI.Page starts with a capital
"P". C# is case-sensitive.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Marc" <ma**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hello.

Thanks for the quick reply. I have created a Namespace.page class which
includes methods for interacting with a database and returning content
for pages and menu, graphic etc. Previously in my controls i would have
said:

public page webpage;
private void page_load(object etc etc etc)
{
page = new webpage(1);
lblHeading = page.getHeading();
lblBody = page.getBody();
dgMenu.DataSource = page.getMenuItems();
dgMenu.DataBind(); (etc etc etc)
}

I want to only create a single instance of this class and call it from
any control so it sounds like:

" If you want to store an instance of a clsss in Application Scope,
your best
bet is to create the instance in one of these event handlers and add it
to
the Application Cache. "

Is what i want to do. I have attempted to do this by:

webpage = new page();
Cache.Insert ("webpage", webpage, null, Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes (15));

in my global.asax.

Now i get a null object reference error - which comes back to what you
said here:

"Second, you have not created an instance of the class, just a variable
for
containing an instance of the class. "

Just i dont understand why it is not an instance of my class? I case of
a little knowledge is dangerous?

Thanks for any help!

ta

marc

Nov 19 '05 #5
thanks tom.

will have a look in due course and get back to you.

cheers,

marc

Nov 19 '05 #6
"Take a look at my first comment. System.Web.UI.Page starts with a
capital
"P". C# is case-sensitive. "

Hello.

I am not sure where this applies as i have created a class called
myNameSpace.page, which is the one i am trying to work with.

Anyway - i think i have managed to inherit from a baseclass. I have a
baseclass of:

public class page : System.Web.UI.Page
{
protected override void OnLoad (EventArgs e)
{
System.Web.HttpContext.Current.Response.Write("mes sage from
baseclass");
}

public string getString()
{
string str = "this is a string";
return str;
}
}

Which i have inherited into my page using <%@ Page Language="C#"
Debug="true" Inherits="NameSpace.page"%>.

Now each page outputs "message from baseclass". But, when i try to use
the getString method i get an error:

"CS1519: Invalid token '(' in class, struct, or interface member
declaration"

Any ideas anyone?

ta

Nov 19 '05 #7
Ignore last one. im an idiot.....

ta

Nov 19 '05 #8
Hi Marc,
"CS1519: Invalid token '(' in class, struct, or interface member
declaration"
Your base class looks fine. Can we see the code for your derived class?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Paranoia is just a state of mind.

"Marc" <ma**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... "Take a look at my first comment. System.Web.UI.Page starts with a
capital
"P". C# is case-sensitive. "

Hello.

I am not sure where this applies as i have created a class called
myNameSpace.page, which is the one i am trying to work with.

Anyway - i think i have managed to inherit from a baseclass. I have a
baseclass of:

public class page : System.Web.UI.Page
{
protected override void OnLoad (EventArgs e)
{
System.Web.HttpContext.Current.Response.Write("mes sage from
baseclass");
}

public string getString()
{
string str = "this is a string";
return str;
}
}

Which i have inherited into my page using <%@ Page Language="C#"
Debug="true" Inherits="NameSpace.page"%>.

Now each page outputs "message from baseclass". But, when i try to use
the getString method i get an error:

"CS1519: Invalid token '(' in class, struct, or interface member
declaration"

Any ideas anyone?

ta

Nov 19 '05 #9
Now, how can you be an idiot if you figured it out for yourself?

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Paranoia is just a state of mind.

"Marc" <ma**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Ignore last one. im an idiot.....

ta

Nov 19 '05 #10
Kevin,

everybody is free to consider themselves as
whatever they wish to consider themselves as... ;-)

But, I do agree with you... !

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:e6**************@TK2MSFTNGP12.phx.gbl...
Now, how can you be an idiot if you figured it out for yourself?

--
;-),
Kevin Spencer
Microsoft MVP
.Net Developer
Paranoia is just a state of mind. "Marc" <ma**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Ignore last one. im an idiot.....

ta


Nov 19 '05 #11
Its because i forgot to put in the private void page_load etc etc etc
doh!

Nov 19 '05 #12

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

Similar topics

10
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
3
by: Faisal | last post by:
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...
2
by: PRTC | last post by:
I'm trying to use the global.asax in my new web aplication proyect using the Application start to store my connection string GLOBAL.ASAX.vb Sub Application_Start(ByVal sender As Object,...
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...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
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: Dylan Parry | last post by:
Hi folks, I'm not really sure of the terminology here, so I'll try my best. I have been using global.asax to set up application variables that are used within my applications, but for obvious...
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...
12
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I am trying to maintain a list of people who are currently "online" in SQL. I do this by adding a simple entry to a simple PeopleOnline table whenever someone logs in to my site. If they...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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)...
1
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...
1
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.