Solved

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

Posted on 2003-11-26
8
515 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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