Problems using application state with custom classes

B

Brett

I am working with vb.net in a asp.net application.
I have created a config file in xml format. The goal is to
be able to change the config file without having to recompile the
entire
application.

So when the Application starts I read the xml file and place
each key-value pair into a Synchronized hash table. Now I want to
save this hashtable to Application state for quick access and also so
I don't have to open the config file a bizzilion times.

The problem comes in when I try to use the Application state from a
custom
class. I have created a custom vars class which holds the methods to
read the XML and build the hashtable, then save it the the application
state. But when I try to save anything into the application state from
this class I recieve a

"System.NullReferenceException: Object reference not set to an
instance of an object."

Error.

I have placed the building of the Application in the default
contructor and each time the constructor is called I get the error.

Here is the code...


Public Class _411Vars
Inherits System.Web.UI.Page

Private config As New Hashtable()

Sub New()
If Context.Application("vars") Is Nothing Then
Context.Application.Lock()
Context.Application("vars") = readXML()
Context.Application.UnLock()
config = CType(context.Application("vars"), Hashtable)
Else
config = CType(Context.Application("vars"), Hashtable)
End If
End Sub

Function c_dbPath() As String
Return CType(config("c_dbPath"), String)
End Function
......
.....
...
..

Now when I try to use the Application state in other web classes it
works just fine.
I get the null exception only when I try to save something to the
Application from a custom class.

I am sure I have missed something some where if anyone can lend a hand
that would be great.

I have read in places that the Application("") call must be in the Sub
Page_Load but I have been successful when using it in other
places??????

Any suggestions would be great

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,211
Latest member
NelleWilde

Latest Threads

Top