views:

138

answers:

1

Hi Guys,

I'm having a bit of trouble with knowing how I can save items from a list I have in Application state in the global.asax as- Application[""].

My controller basically takes in some user input, I then pass this to another classes Method as parameters it gets added to a list all the time. This data thats getting added to the list I want to store it, but without using a db. By using Application State. . I have been instantiating this class and calling its method to return the list of items but I dont think Application State is saving it.

Here is what I have so far. .

        protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);


        TimeLineInformation t = new TimeLineInformation();
        IList<SWTimeEnvInfoDTO> g = t.getInfo();

        Application["AppID"] =  g;
    }

^ Global.asax

        IList<SWTimeEnvInfoDTO> result = new List<SWTimeEnvInfoDTO>();

    public  void returnTimeLineInfo(string SWrelease, string EnvName, DateTime SDate, DateTime EDate) {


        SWTimeEnvInfoDTO myDTO = new SWTimeEnvInfoDTO();
        myDTO.softwareReleaseName = SWrelease;
        myDTO.environmentName = EnvName; 
        myDTO.StartDate = SDate;
        myDTO.EndDate = EDate;

        result.Add(myDTO);

        getInfo();


    }


    public IList<SWTimeEnvInfoDTO> getInfo() {

        return result;

    }

^ class im calling

The SWTimeEnvInfoDTO type has get and set methods for the data.

I am calling the application from a View as well. It works with a string

Application["AppID"] = "fgt";

and shows this once i read it from my view.

+1  A: 

Updated: You should use casting to retrieve variable you have saved at Application, and save it if it has changed:

IList<SWTimeEnvInfoDTO> result = Application["AppID"] != null ? (IList<SWTimeEnvInfoDTO>)Application["AppID"] : new List<SWTimeEnvInfoDTO>(); // retrieve data  

result.Add(...);               // add some new items
Application["AppID"] = result; // save added values
SergeanT
Thanks, I'm still debigging though and its not returning new items I've added in the list to the Application state for storage, it reads the initial value which is null but as I add more items, nothing . .?
Calibre2010
You should 1) extract current value from app storage 2) make changes 3) put changed value to app storage
SergeanT