locked
Multiple Dimensional Array in a Session RRS feed

  • Question

  • User1763161047 posted

    I need a little direction here.  I have a need to track a shopping cart type information while the user builds the order.  The cart data would have four columns of data to keep track of with a single record (Key).  So what I have been trying to research is a way to contaminate the string and then be able to break down the string in the read on the next page using Session Variable Table to store the information.

    So here is what I am attempting to do in pseudo code.

    Create a multi-dimensional array using a Hashable Table or a System Array. 

    Create the Table with the following information.  

    CartTable.Add("Key","ProductType","ProductID","AgentID") = intKey + strProductType + strProductID+ strAgentID  'This would create a single indexed record at index zero with the fields listed.
    CartTable.Add("Key","ProductType","ProductID","AgentID") = (intKey + 1) + strProductType + strProductID+ strAgentID 'This would index to the next key.
    Session("Cart") = CartTable

    Then to read the data.

    Dim Key as Integer = 0

    label1.text = Session("Cart") (Key,"ProductID") 
    lable2.text = Session("Cart")((Key+1)"ProductID")

    This would then clear the cart.

    Session("Cart") = Clear

    ----------

    So that is what I have been trying to figure out.  I see lots of examples of Sessions and Hashable Tables but none show how to store more than one value in a single key.  If I stored different information in a new key I would have no way to determine what key went with what record.

    I think this breaks down to two three possible directions. 

    1.  Contaminate a string in the session variable in the same why that you would store and retrieve information using a QueryString which can have multiple values in a single string and can be addressed individually in a read statement.

    2.  Need to figure out the syntax for a multidimensional array that can be stored in a Session Variable.

    3. It cannot be done.  Just create a SQL table and store the items there and then empty the table with a method that uses the users Session Key as the record ID so that I can clear only that users records. 

    Thank you all for your help.

    Mike

    Monday, April 10, 2017 3:44 PM

All replies

  • User753101303 posted

    Hi,

    You don't need to store a string. You can store objects in a session. Make sure to hide that behind a facade.

    That said I would likely go for a db table as you can then turn the cart into a wish list or one could retrieve his cart on the next day etc...

    Monday, April 10, 2017 3:52 PM
  • User-2057865890 posted

    Hi Mike,

    I see lots of examples of Sessions and Hashable Tables but none show how to store more than one value in a single key.

    Since each element of the Hashtable is a key/value pair, the element type is not the type of the key or the type of the value. Instead, the element type is DictionaryEntry. 

    Adds an element with the specified key and value into the Hashtable.

    var mydata = new List<item>
    {
        new item { ProductType = 1, ProductID = 1, AgentID = 1 },
        new item { ProductType = 2, ProductID = 2, AgentID = 2 },
    };
    
    Hashtable ht = new Hashtable();
    ht.Add("1", mydata);
    
    Session["Cart"] = ht;

    Read the data

    Hashtable h = (Hashtable)Session["Cart"];
    List<item> data = (List<item>)h["1"];

    Best Regards,

    Chris

    Tuesday, April 11, 2017 6:10 AM