Solved

Where to store data in ASP.Net

Posted on 2013-11-12
13
387 Views
Last Modified: 2013-11-12
I need to store a value in ASP.net web application that is assessable "out of band".  The application has processes that run on the timer event, and during these times, there may not be a Request context available.  Is there somewhere I can store a variable since the App or Session cache is not available to me?
0
Comment
Question by:No1Coder
13 Comments
 
LVL 33

Expert Comment

by:paulmacd
Comment Utility
You can store data in a database of some sort.  If that's too extravagant, perhaps just saving the info in a text file will do.

As for the client side, you can usually set a cookie.
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
Comment Utility
You can use Static class's static property to store them and access at later point of time.


using System;
using System.Data;
using System.Linq;
using System.Web;

public static class MyClass
{
    static string _importantData;

    public static string ImportantData
    {
	get
	{
	    return _importantData;
	}
	set
	{
	    _importantData = value;
	}
    }
}


//Set the value MyClass.ImportantData = "my data"; and get it later point of time.

Open in new window




If the data are more you could probably choose the option to store data in database such as MSSQL or Access. It depends on your requirement.

If you prefer for file storage I suggest to store and retreive as XML file instead of Flat file.
0
 

Author Comment

by:No1Coder
Comment Utility
The static approach is very interesting.  Where is this data stored?  Is it visible everywhere?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
If you use a static it will be stored in the App Domain (i.e. the ASP.Net Worker process). If the process gets recycled you will lose the value.

So whether you take that approach or not will depend on how long you need the value to persist.
0
 

Author Comment

by:No1Coder
Comment Utility
Ok.  What is the visibility?  Is it all processes?  App Pool related?

I need it to be application wide (all sessions running this app), but not all applications running on this pool.
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Static is scoped to the application, same as if you set an Application variable (which personally I would do instead of using a static as it is clearer what the intention was). So, no, it won't be shared by other applications in the same Pool.
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:No1Coder
Comment Utility
How would I access the Application object from a static method that is called out of bounds?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
What do you mean by "out of bounds"? If you mean from a third-part process then you can't. You would need to persist the value to disk, or a database, or somewhere to access it.
0
 

Author Comment

by:No1Coder
Comment Utility
As I explained in my original question, a process running off of the timer event.  No request context available.
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
If that timer is running within the context of your web app then you should still be able to get to the context using something like:
string someValue HttpContext.Current.Application["YourVariable"];

Open in new window

That would require you to store your value in an Application variable though, rather than using a static.
0
 

Author Comment

by:No1Coder
Comment Utility
What I discovered is that when running off the timer, there is no HttpContext available (it is null).  Otherwise, I would use the Application cache.

So, if this is not available, will the static variable method work?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
It should do. I used this test code:
    public static class Members
    {
        public static string Value;
    }

    public class Global : System.Web.HttpApplication
    {
        System.Threading.Timer timer = null;

        protected void Application_Start(object sender, EventArgs e)
        {
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(Timer_Callback), null, 60000, 60000);
        }

        protected void Timer_Callback(object state)
        {
            string value = Members.Value;
        }
    }

Open in new window

And set the variable from a page, and I could retrieve it ok when the timer fired.
0
 

Author Comment

by:No1Coder
Comment Utility
Okay.  I implemented this.  Thanks a lot for your assistance.  This is much easier than the alternative.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

14 Experts available now in Live!

Get 1:1 Help Now