Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I save an array in ASP.NET's Application state using C#.
Posted
Updated 18-May-15 21:45pm
v2
Comments
Kornfeld Eliyahu Peter 19-May-15 3:21am    
Are you talking about a web application?
Member 9361273 19-May-15 3:35am    
no i am talking about application state in asp.net project that is: Application["sdfsdg"]= "dfgdfG"
Kornfeld Eliyahu Peter 19-May-15 3:48am    
asp.net IS web application :-)
BillWoodruff 19-May-15 3:35am    
To assist you, we need more details. Use tags on your question to communicate what the context is.
Member 9361273 19-May-15 3:47am    
Application state is one of the ways available to store some data on the server and faster than accessing the database.
void Application_Start(object sender, EventArgs e)
{
Application["Message"] = "Welcome to my Website";
}

The data stored on the Application state is available to all the users(Sessions) accessing the application

1 solution

Yes, you can store the values in Application state. Application state stores the value in Object type. So, you can store any kind of data in the state. Provided that you properly cast it back to the actual type. Like this,

C#
string[] stringArray = { "Afzaal", "Ahmad", "Zeeshan" };
Application["StringArray"] = stringArray;


For more on storing the values in Application state please read: https://msdn.microsoft.com/en-us/library/94xkskdf(v=vs.140).aspx[^]

Now when you want to extract the values, you simply use this...

C#
if(Application["StringArray"] != null) {
   // checking is safer than not checking and getting NullReferenceException
   // casting to string[]
   string[] stringArray = (string[])Application["StringArray"];
   // get the value
   string surname = stringArray[2]; // "Zeeshan"
}


For more on extracting the values from Application state please read: https://msdn.microsoft.com/en-us/library/y8hhek39(v=vs.140).aspx[^]

Checking if the key/value pair exists or not doesn't cost a fortune. But will surely protect you from NullReferenceException. Then, you can cast the value to the proper type (which so ever type it is... Even custom types such as class or struct etc.). They would work. :-)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900