Hi I have a static class that has static variables that contain data from the database. I want these variables to be updated every 5 seconds so I created a thread, inside the same class, which will execute a function that will fetch data from the database and update the variables. But I get a nullReferenceException pointing to the line where I declare initialize my connection.
Here is my code:
public static void Initialize()
{
if (!isInitialized)
{
isInitialized = true;
Thread t = new Thread( new ThreadStart(SetProperties));
t.Start();
}
}
public static void SetProperties()
{
//The next line is where the NullReferenceException is pointing to
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + System.Web.HttpContext.Current.Server.MapPath("cms.accdb") + "';");
using (conn)
{ ....
Any help, please? Thanks
HttpContext.Currentoutside of the request thread. Seriously, what you're doing is silly. You need to rethink the design a lot, ASP.NET isn't really the environment for doing something like this.HttpContext.Currentis context-sensitive. It will only have a value on the request thread, and you left that by starting a new thread (that is, yourInitializemethod runs on the request thread, butSetPropertiesdoesn't). Which causes yourNullReferenceException, because you're trying to access properties of anullvalue. Caching is one of the hardest things in computing, so you might want to learn a bit more before attempting anything serious in that regard. Multi-threading is another of the hardest things, and here you're doing both as a beginner. Bad idea :)