Solved

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

Posted on 2003-11-26
8
514 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
ID: 9825671
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
ID: 9830249
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
ID: 9831047
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 200 total points
ID: 9831488
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
 

Author Comment

by:Smoerble
ID: 9831663
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
ID: 9835923
> 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
ID: 9836666
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
ID: 9859541
I am flattered of the grade....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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