Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using application variables - global.asax

Status
Not open for further replies.

sodakotahusker

Programmer
Mar 15, 2001
601
I have never done this before but this appears to be a desirable route. If I have content for drop down lists which rarely changes - would it be feasible (and possible) to read the data from the database in the global.asax file and populate an array or arraylist or collection - whatever makes more sense - and then populate and dropdowns needing that list using the application variable instead of going back to the database every time the pages needing that data are loaded? I have not seen a sample of writing code behind in the global.asax file - but I am assuming you can do that? Can anyone poke some holes in my theory or give me some backup - before I wade in and spend time on a proof of concept?
 
Better to use the Data Cache:
Code:
...Page Load
DataView dv = (DataView)Cache["myView"];
if(dv == null) // not there
{
//connection, DataAdapter, DataSet(ds) stuff here....
dv = ds.Tables["yourTable"].Default.View;
dv.AllowEdit=false;
dv.AllowNew=false;
dv.AllowDelete=false;
Cache["myView"] = dv;
}
else
{
DropDownList1.DataSource=dv;
DropDownList1.DataTextField="whatever";
DropDownList1.DataValueField="whateverElse";
DataBind();
}
 
Even better: use Veep's ideea in global.asax under the Application_Start event. I've used this thechnique for more than a year and never had problems. The only drawback is that if the dropdownlist's datasource changes, you have to restart the application in order to get the changes. So it's a matter of needs - if the datasource gets modified often, better put the Cache source logic under the Session_Start event, otherwise, put it under the Application_Start.
Hope this helps!

[morning]
 
Thanks guys! I got this working with one of my dropdowns. One more question though - Since my site is hosted by a Host Provider - I do not have access to IIS manager to stop an application. Is there a way I can stop it from an ASP page - or more likely to remove my cached dataviews from the cache?
 
I'm getting myself in deeper.

I figured - how about I create a class method that refreshes all of the cache items. And I'll call that from global.asax as well as from all of the pages that need the cache items. So I created the class and put in the method and life looked good - but when I loaded a page I got a message that the cache was not available. Looks like it has to be an aspx page in order to access the cache object?

In order to get the cache to be recognized in the class module I had to include

Inherits System.Web.UI.Page

I can't successfully do that with global.asax - so it appears to me when you recommend using the Application_start in global.asax, you are referring to use of the application object and not the cache object?

Bottom line: I'd like to put the code out there once to do this caching - but it appears I'll have to put it in every page - cause if something happens and the cache gets lost (without the app stopping) - I'll have to refresh it from the page since the on_start will not fire again.

That just leads me to the question - what kind of restrictions do I run into trying to use a class module with common routines in it?
 
Google Cache.Insert() which has several overloaded methods. This will allow you to set the expiration of the Cached data. I believe that you're always going to write a few lines of code at the page level whether your implementation is in a class or not. You're always going to need to test whether the Cache is really there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top