[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2003-11-26
8
Medium Priority
?
519 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 40 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 400 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 60 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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