Im facing a problem in one of MVC projects, that problem is about retrieving values from Application State object.
I am storing some value into the Application state in Application_Start()
method of Global.axas.cs
in this manner:
//var str = Obj.DecryptString(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
//Application["connString"] = str;
Application["connString"] = Obj.DecryptString(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
I have made sure the value from web.config
has been successfully retrieve at this point through debugging.
After this step I’m trying to retrieve this value in one of my Model Class
in this manner:
var conn = new SqlConnection(Application["connString"].ToString());
But here I’m getting a NullReferenceException
Object reference not set to an instance of an object.
Now this is very confusing for me here that if the value has been successfully retrieved and stored in AapplicationState
at the point of Application_Start()
than why isn’t it available in my model class, aren't the ApplciatiopnSate available throughout the application?