Solved

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

Posted on 2003-11-26
8
511 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 70
Run time Error 4 47
Diagnostics with Net and Net.Sockets 2 26
How useful is the free version of Selenium? 3 31
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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