Hashtable implements shopping cart and discards the database Implementation Method

Source: Internet
Author: User
Previously, our shopping cart was implemented in the database mode, and database operations were required for each purchase, which greatly affected the efficiency. Now we use hashtable and session to implement shopping cart, which saves database operations and greatly improves efficiency.

1 private void datagrid1_itemcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs e) // assume that the previous command is a linkbutton named buy.
2 {// key: Create and add a shopping cart
3 string pid = This. datagrid1.datakeys [E. Item. itemindex]. tostring (); // retrieve the ID
4 If (E. commandname = "buy") // If the command name is buy, purchase
5 {
6 if (session ["bus"] = NULL) // check whether the shopping cart exists. If not, create
7 {
8 system. Collections. hashtable ht = new hashtable (); // create a hash table first.
9 Ht. Add (PID, 1); // two columns in the hash table, one key, one value, and one key, and the number of the purchased values, are set to 1.
10 Session ["bus"] = HT; // assign a hash table to the Session Object
11}
12 else // If yes
13 {
14 hashtable ht = (hashtable) session ["bus"]; // use forced type conversion, and then assign session ["bus"] To the hash table object HT
15 if (HT [pid] = NULL) // if the corresponding ID in the hash table does not exist,
16 {
17 HT [pid] = 1; // set it to 1 directly.
18}
19 else // if the corresponding ID already exists
20 {
21 HT [pid] = (INT) HT [pid] + 1; // Add 1 to the original one.
22}
23 session ["bus"] = HT; // finally update the session object
24}
25}
26
27}

The read method is as follows:

This. datalist1.datasource = (hashtable) session ["bus"];
This. datalist1.databind ();

1 private void linkbutton#click (Object sender, system. eventargs e)
2 {
3
4 foreach (datalistitem DL in this. datalist1.items) // traverses the set
5 {
6 textbox TB = (textbox) dL. findcontrol ("textbox1"); // find the text box
7 int newpid = convert. toint32 (TB. text. tostring (); // check the value in the text box
8
9 label label1 = (Label) dL. findcontrol ("key"); // find the control that loads the key field of the hash table
10 string pid = label1.text. tostring (); // Extract the value
11
12 hashtable ht = (hashtable) session ["bus"]; // assign the session ["bus"] object to the hash table HT
13 int oldpid = (INT) HT [pid]; // obtain the original quantity
14
15 if (newpid! = Oldpid) // if the value in the text box is not equal to the original quantity, replace the new hash table value with
16 {
17 HT [pid] = newpid;
18}
19 session ["bus"] = HT; // last update Session Object
20}
21}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.