locked
Session & Cache objects, howto RRS feed

  • Question

  • User1111434469 posted

    Hello,

    I have difficulties structuring my logic when using Session and Cache objects. I want to use Session and Cache in order to manage my users experiece. Please help me construct the logic.

    I'll try to explain what my user do in the website:

    1. User enters the webpage.

    2. Select and write something in my Filters1 (textboxes, comboboxes, checkboxes) to search.

    3. After user made the desired selections in Filters1, he clicks a Button1 'Search button'.

    4.1 Search results are returned to the user in asp:ListView (this is 30 columns, 20-200 rows of data). These results are returned from HTTPWebRequest in a third party website and formated into a DataSet / DataTable.

    4.2. Additional Filters2 (sort, group, paging) appear for the user to additionaly filter only the returned search results.

     

    5. User use Filters2 (sort, group, paging) in step4.2. and if the user don't like the returned results, he make a new search going through step1 to step 4.2 again.

     

    I want :

    1. A new Session (SessionID) for each user to be created on Button1_Click

    2. The returned results from Filters1 to be stored in Cache object (DataSet). This DataSet is only for the current user.

    2.1. If user decides to go through step1 to step 4.2 again, before the Session expires, the SessionID should stay the same, but the Cache object should be Clear() and filled in with a new DataSet.

    2.2. If user Session expires, the Cache object should clear / expire too.

     

    How should I construct my if...else statement with Session & Cache objects in Page_Load and Button_Click events?

    Wednesday, February 9, 2011 6:17 AM

Answers

All replies

  • User236141260 posted

    After first search save your dataset/datatable in viewstate, this if user apply second search get dataset/datatable from viewstate cast it in dataset/datatable and apply filter on it.

    If user starts new search then again get data from third party and save dataset/datatable in viewstate.

    this will also store data for each user differently.

    Wednesday, February 9, 2011 6:42 AM
  • User1111434469 posted

    Yes, this could work but I think storing DataSet of ~1mb (30columns, 20-200 rows of normal strings data) in a ViewState is not so good idea? 

    Wednesday, February 9, 2011 6:49 AM
  • User236141260 posted

    Save your search criteria in viewstate or session object, on first search fill this criterial objects with criteria fields, on second search(sorting) get values from criteria and apply filters. If user apply first search again refresh criteria objects with new criteria fields.

    If you want more explanation please let me know.

    Wednesday, February 9, 2011 7:16 AM
  • User1111434469 posted

    I have a little problem recreating this explanation in code:

    For example:

    Filters1: DropDownList1 (first search)

    Filters2: TextBox1 (second search)

    private void Button1_Click(object sender, EventArgs e)
    
    {
    
    if ( HttpContext.Current.Session[HttpContext.Current.Session.SessionID] == null)
    
    {
    
    HttpContext.Current.Session[HttpContext.Current.Session.SessionID] = HttpContext.Current.Session.SessionID + DropDownList1.Text;
    
    this._fResults = HttpRuntime.Cache[HttpContext.Current.Session.SessionID + DropDownList1.Text] as DataSet;
    this._fResults.Tables.Add(MyGlobals.dt.Clone());
    
    this._fResults.Tables[MyGlobals.n].Columns.Add("Price");
    
                this._fResults.Tables[MyGlobals.n].Clear();
                for (int x = 0; x < MyGlobals.AirlineName1.Count; x++)
                {
                    DataRow dr = this._fResults.Tables[MyGlobals.n].NewRow();
                    dr[0] = MyGlobals.Price1[x];
                    this._fResults.Tables[MyGlobals.n].Rows.Add(dr);
                }
    
    
    HttpRuntime.Cache[HttpContext.Current.Session.SessionID] = this._fResults;
    
    }
    
    else
    
    {
    
           //if Session expires or TextBox1 search
           HttpRuntime.Cache.Remove(HttpContext.Current.Session.SessionID + DropDownList1.Text);
    
           this._fResults = null;
           this._fResults = HttpRuntime.Cache[HttpContext.Current.Session.SessionID] as DataSet;
           this._fResults = new DataSet();
           this._fResults.Tables.Add(MyGlobals.dt.Clone());
           HttpRuntime.Cache[HttpContext.Current.Session.SessionID] = this._fResults;
    
    }
    }


     

     

     

    Wednesday, February 9, 2011 7:42 AM
  • User-1184016586 posted

    Hi,


    in this senario , i thgought caching is better, create one user control, place all controls in that, put caching propery client side or server side, i hope server side is bet.then it wil be give good performanceor use data caching.



    regards

    venki






    Wednesday, February 9, 2011 8:03 AM
  • User1111434469 posted

    The problem is that the code I wrote above is not good. It doesn't recreate the logic from my first post in the topic & Waqar_ali :
    --
    Save your search criteria in viewstate or session object, on first search fill this criterial objects with criteria fields, on second search(sorting) get values from criteria and apply filters. If user apply first search again refresh criteria objects with new criteria fields.
    --

    I want to use Session object for the Filters1 criteria (DropDownList1) and Cache object for Filters2 (TextBox1) criteria.

    The problem is that I can't successfully code what is written in words, please help.

    Wednesday, February 9, 2011 8:13 AM
  • User-660870441 posted

    Hi,

    It seems that it is a duplicate as Session, Cache, please give me simple code example. Please also check replies in that thread.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 13, 2011 10:26 PM