views:

83

answers:

3

I am getting this error when I converting Application level variable into Hashtable;

First In Global.aspx file I have store Hashtable into Application variable like that:

Hashtable htErrorDescription = new Hashtable();
htErrorDescription.Add("Error 1","General Error");
htErrorDescription.Add("Error 2","Service Error");

context.Application["htErrorDescription"] = htErrorDescription + "";

After that I want get this Hashtable from Application level variable back.

Hashtable htOutput;
htOutput = (Hashtable)context.Application["htErrorDescription"];

I am getting this Error.

Please help me out of this error, Thanx in advance.

+3  A: 

When you do:

Application["Application["htErrorDescription"] = htErrorDescription + "";

It is converting the htErrorDescription to a string implicitly. You need to get rid of the:

+ ""

Also, I don't think this even compiles, aren't you missing a closing ] character, and you're nesting quotes without using a backslash to escape them?

EDIT: I see you edited your question, so all you need to do now is remove the + "".

dcp
A: 

Put the object into Application like this:

context.Application["htErrorDescription"] = htErrorDescription;
Crassy
+1  A: 

First, stop concatenating the dictionary with a string, as this means that what you are storing in the application collection is the result of Hashtable.ToString() which is just the string "System.Collections.Hashtable".

That's just a slip-up though, your big problem is that you are storing a dictionary of errors from a particular operation in application scope.

Consider two people (Alice and Bob, just to be traditional) accessing the web application at the same time. IIS takes the two requests from its request queue, assigns a worker thread to handling each of the requests, and so far all is well.

Alice's operation checks her account is paid up. It isn't so an error table is created, we add an error with the description "Account not paid for", and assign it to the application-scoped variable.

Meanwhile, Bob's operation is trying to create a business meeting, but he's made a mistake and set it for last month, so an error saying "You can't create a business meeting in the past" is assigned to a new table of errors, and assigned to the application-scoped variable.

These are happening at roughly the same time, so its essentially random as to what is now in the errors table.

Alice is now presented with the error message. Maybe it's the right one, or maybe its nonsense about business meetings in the past that mean nothing to her. There's a good chance Alice complains to tech.

Meanwhile Bob is also presented with the error message. Maybe it's the right one, or maybe its complaining he hasn't paid his bills, when he knows for a fact that he has. There's a good chance Bob complains to invoicing or his direct-liason in the company running the website. This is likely to be worse than him complaining to tech.

If you need to pass an object to a later part of the processing of the same web request, use the Items property of the HttpContext.

Jon Hanna