Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 10th, 2009, 02:24 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context

Hi,

I have a MENU user control being referenced by all the web pages in the applicaiton. The menu control is dynamic and menu items are extracted from the database table. In this process, for each web page to load the menu control, database is hit.

I would like to store the menu items from teh database in an APPLICATION CONTEXT at the start itself.

Any suggestions as how can i accomplish this?

Thanks
  #2 (permalink)  
Old December 10th, 2009, 10:35 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Why not use the Cache?

http://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx
http://www.4guysfromrolla.com/articles/100902-1.aspx

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #3 (permalink)  
Old December 10th, 2009, 10:45 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

Thanks a lot.

Can u please elaborate a bit and let me know how and through which page i can implement this? Will this be a part of global.asax file?

Please let me know about it.

thanks.
  #4 (permalink)  
Old December 10th, 2009, 10:59 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You could certainly put it there, although I favor putting things like this on my MasterPages. If you do attempt to handle this in the global.asax I would probably handle it in the Session_Start event. Then on every page you would need to see if the item is still in Cache and, if it is, pouplate your menu with the Cache data otherwise make your database call.

The cache has to expiration modes: absolute and sliding. In the case of sliding the max time limit is 10 mins where as an absolute expiration can be anything really. This example shows you how to work with Caching in some detail:

http://msdn.microsoft.com/en-us/library/ms972379.aspx

The example is in VB.NET but it is trivial to convert it to C#.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #5 (permalink)  
Old December 10th, 2009, 11:18 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

I have used <%@ OutputCache %> directive. How is it related to caching? Also i have a user control called: Menu (ID: mnuMenu). That men items and sub items are being populated through the database. I want this to happen only once.

So should i be writing a code in Session_Start event of Global.asax. (I am not using the master pages).

Can you provide some small code snippet for better idea.

Thanks
  #6 (permalink)  
Old December 10th, 2009, 11:37 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

<%@ OutputCache %> is a page level directive. It will cache the contents of X page for a specificed amount of time. This is typically done to reduce the number of calls to a database or on a page that does not change or changes very slowly (a company's About Us page would be a good example of a page that changes very slowly or not at all).


Since you are using a user control, I might do something like this in the user control:

csharp Code:
protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack) LoadMenu();
}
 
private void LoadMenu()
{
   if(Cache["Menu"] == null) LoadData();
 
   Menu.Data = Cache["Menu"];
}
 
private void LoadData()
{
   //Database Operation
   Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero);
}

The over all flow is straightforward so I am not going to explain it. The "magic" happens here: Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero); This inserts your Menu data into the cache for one day at which time it will expire and another database call will be made. You do not need to use the global.asax at all.

Now, there is a trade off. You are reducing calls to your Database server but you are putting extra load on the Webserver. Inserting stuff into Cache consumes server memory so it is not wise to insert LARGE amounts of data into the cache. In most normal uses you wont notice an issue or with performance on the server but be aware that this *could* impact server preformance.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
  #7 (permalink)  
Old December 10th, 2009, 11:58 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

What does Cache["Menu"] means here? I mean what does "Menu" stands for? is it some kind of a session variable or where is the value fr it being stored?

Thanks
  #8 (permalink)  
Old December 10th, 2009, 12:08 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It is a Key. The Cache is nothing more than Key/Value pairs. In this line:

Cache.Insert("Menu", <your data>, null, DateTime.Now.AddDays(1), TimeSpan.Zero);

The first parameter, "Menu", sets the key for the item in the cache. If I change "Menu" in the above line to "MySuperAwesomeMenu" then all the instances of Cache["Menu"] would need to be replaced with Cache["MySuperAwesomeMenu"].

hth
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
The Following User Says Thank You to dparsons For This Useful Post:
irProject (December 10th, 2009)
  #9 (permalink)  
Old December 10th, 2009, 02:21 PM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

I still didnt get wheere we provide the value for "Menu". where is the name value pair defined?

Also my menu also has submenu's. the menu looks like:

Home
Import
Export
Reports
Report1
Report2
Logout

the code piece u sent refers to some key value pairs. but where do we define the value for that? also how do we check fr Menu.Data that u have mentioned. :(

Please help.
  #10 (permalink)  
Old December 11th, 2009, 10:51 AM
Authorized User
 
Join Date: Dec 2009
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Application context in asp.net web application

Is there any way we can cache the menu items at an application level rather than in cache. Since cache is session/user specific, we want to store the menu at the application level.

any suggestion please?

Thanks
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I map multiple context roots to a single web application? massterb J2EE 1 November 29th, 2009 01:47 AM
how to set context in web application [PLEASE HELP] arashdx J2EE 3 January 27th, 2009 03:53 AM
Application Context in C#.net dineshpdn Visual Studio 2005 0 October 10th, 2007 06:01 AM
A Chat Application using Context and Sessions kotaiah J2EE 0 October 10th, 2006 09:56 PM
How to create context help in web application? Alexds ASP.NET 2.0 Basics 1 February 1st, 2006 06:23 PM



All times are GMT -4. The time now is 05:48 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.