Solved

C#: (urgent) how to check if Application variable exists?

Posted on 2003-11-26
8
503 Views
Last Modified: 2009-12-16
Hi all...
I want to store the result of a database query in an Application variable as some kind of caching.

For this I need to check if this Application variable is already set. Can some please give me the correct code based on the "pseudo-code" below?


if (Application["databaseResultCache"] = null) {
    // an SQL query here
    Application["databaseResultCache"] = "whatever";
}


Thank you
0
Comment
Question by:Smoerble
8 Comments
 
LVL 4

Assisted Solution

by:astankovic
astankovic earned 20 total points
Comment Utility
Your pseudo code should do it,  i just added ==

if(Application["databaseResultCache"] == null)
{
  //query database
  Application["databaseResultCache"] = ds;
}
else
{
  //use cached
}
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 200 total points
Comment Utility
Create a new class and put this code in it:

public class MyCache {
  private DataSet ds = null;
  public static GetDatabaseResultCache() {
    if (MyCache.ds == null) {
      // Query database
      MyCache.ds = ready dataset;
    }

    return MyCache.ds;
  }
}

Then in your application everywhere you can use:

MyCache.GetDatabaseResultCache()

which will work perfectly.
0
 

Author Comment

by:Smoerble
Comment Utility
Sorry, for my lack of understanding...
I wanted to use the Application variable to use the returned DataTable for all users. If I work with the code from "TheAvenger", will the DataTable also be accessable for all users from "cache"?
0
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 200 total points
Comment Utility
The Application object is of a specific type, coming with the .net framework and you cannot add properties the way you want.

The way I showed you you can put whatever properties you want. If you meant the table inside the data set, you can always access it by using:

MyCache.GetDatabaseResultCache().Tables["TableName"];
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Smoerble
Comment Utility
Hmm... seems I did not explain good enough what I needed to do...
Anyways the solution of "astankovic" is ok for me (implemented, tested and works).

So if this is working, I don't understand what the advantage of the solution from "TheAvenger" is?
0
 
LVL 1

Assisted Solution

by:AJMartin
AJMartin earned 30 total points
Comment Utility
> I don't understand what the advantage of the solution from "TheAvenger" is?

The advantage is that TheAvenger's code doesn't repeat itself. Consider in a larger program, whenever the program needs the cached result, in your program, the pseudo-code from astankovic will be repeated a number of times. With TheAvenger's code, you only need to write:
MyCache.GetDatabaseResultCache().Tables["TableName"];
Which is noticably shorter than writing repeatedly:
if(Application["databaseResultCache"] == null)
{
  //query database
  Application["databaseResultCache"] = ds;
}
else
{
  //use cached
}

:)
0
 

Author Comment

by:Smoerble
Comment Utility
Sorry, still don't get it (because of my stupidity increased points to 350):
I don't see the "ApplicationVariable" in the code from TheAvenger... but I need to share the same data between several scripts/files, so my assumtion was I have to use the "ApplicationVariable".

Is this true?
0
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
I am flattered of the grade....
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now