473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

shopping cart

Hi,

I having problem in adding the variable into linkedlist. Can anyone guide me?

Expand|Select|Wrap|Line Numbers
  1. <div id="one" style="display: block; ">
  2.                         <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
  3.                             GroupItemCount="3" OnItemCommand="ListView1_OnItemCommand">
  4.                             <EmptyItemTemplate>
  5.                                 <td runat="server" />
  6.                             </EmptyItemTemplate>
  7.                                 <ItemTemplate>
  8.                                     <td runat="server" style="">
  9.                                             <asp:Label ID="foodnameLabel" runat="server" Text='<%# Eval("foodname") %>' />
  10.                                             <br />
  11.                                             <asp:Label ID="foodimgLabel" runat="server" Text='<%# Eval("foodimg") %>' />
  12.                                             <br />
  13.                                             <asp:Label ID="priceLabel" runat="server" Text='<%# String.Format("{0:0.00}", Eval("price")) %>' />
  14.                                             <br />
  15.                                             <asp:Label ID="stallidLabel" runat="server" Text='<%# Eval("stallid") %>' Visible="false"/>
  16.                                             <br />
  17.                                             <asp:Button ID="addtocart" runat="server" Text='Add to cart' CommandName="addtocart" CommandArgument='<%# Eval("foodname")+ " " + Eval("price")%>' />
  18.                                     </td>
  19.                                 </ItemTemplate>
  20.                                 <AlternatingItemTemplate>
  21.                                     <td runat="server" style="">
  22.                                         <asp:Label ID="foodnameLabel" runat="server" Text='<%# Eval("foodname") %>' />
  23.                                         <br />
  24.                                         <asp:Label ID="foodimgLabel" runat="server" Text='<%# Eval("foodimg") %>' />
  25.                                         <br />
  26.                                         <asp:Label ID="priceLabel" runat="server" Text='<%# String.Format("{0:0.00}", Eval("price")) %>' />
  27.                                         <br />
  28.                                         <asp:Label ID="stallidLabel" runat="server" Text='<%# Eval("stallid") %>' Visible="false"/>
  29.                                         <br />
  30.                                         <asp:Button ID="addtocart" runat="server" Text='Add to cart' CommandName="addtocart" CommandArgument='<%# Eval("foodname")%>'  />
  31.                                     </td>
  32.                                 </AlternatingItemTemplate>
  33.                                 <EmptyDataTemplate>
  34.                                     <table runat="server" style="">
  35.                                         <tr>
  36.                                             <td>
  37.                                                 No data was returned.</td>
  38.                                         </tr>
  39.                                     </table>
  40.                                 </EmptyDataTemplate>
  41.                                 <LayoutTemplate>
  42.                                     <table runat="server">
  43.                                         <tr runat="server">
  44.                                             <td runat="server">
  45.                                                 <table ID="groupPlaceholderContainer" runat="server" border="0" style="">
  46.                                                     <tr ID="groupPlaceholder" runat="server">
  47.                                                     </tr>
  48.                                                 </table>
  49.                                             </td>
  50.                                         </tr>
  51.                                         <tr runat="server">
  52.                                             <td runat="server" style="">
  53.                                                 <asp:DataPager ID="DataPager1" runat="server" PageSize="12">
  54.                                                     <Fields>
  55.                                                         <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
  56.                                                             ShowNextPageButton="False" ShowPreviousPageButton="False" />
  57.                                                         <asp:NumericPagerField />
  58.                                                         <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
  59.                                                             ShowNextPageButton="False" ShowPreviousPageButton="False" />
  60.                                                     </Fields>
  61.                                                 </asp:DataPager>
  62.                                             </td>
  63.                                         </tr>
  64.                                     </table>
  65.                                 </LayoutTemplate>
  66.                                 <GroupTemplate>
  67.                                     <tr ID="itemPlaceholderContainer" runat="server">
  68.                                         <td ID="itemPlaceholder" runat="server">
  69.                                         </td>
  70.                                     </tr>
  71.                                 </GroupTemplate>
  72.                             </asp:ListView>
  73.                             <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  74.                                 ConnectionString="<%$ ConnectionStrings:foodcourtConnectionString %>" 
  75.                                 SelectCommand="SELECT [foodname], [foodimg], [price], [stallid] FROM [foodtbl] WHERE ([stallid] = @stallid)">
  76.                                 <SelectParameters>
  77.                                     <asp:QueryStringParameter DefaultValue="1" Name="stallid" 
  78.                                         QueryStringField="stallid" Type="Decimal" />
  79.                                 </SelectParameters>
  80.                             </asp:SqlDataSource>
  81.  
  82.                     </div> 
How can I add all the variable (foodname, price) into linkedlist when I click addtocart button?
Aug 17 '10 #1

✓ answered by Frinavale

I still think that Session is the best way to go.
It's pretty easy.

Create a "FoodItem" class that will contain information about the food being bought:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class FoodItem
  2.   Private _name As String
  3.   Private _price As Double
  4.   Private _quantity as Integer
  5.   Private _description As String
  6.  
  7.   Public Name Property As String
  8.     Get
  9.      return _name 
  10.     End Get
  11.     Set(ByVal value As String)
  12.       _name = value
  13.     End Set
  14.   End Property
  15.  Public Price Property As Double
  16.     Get
  17.       return _price 
  18.     End Get
  19.     Set(ByVal value As Double)
  20.       _price = value
  21.     End Set
  22.   End Property
  23.   Public Quantity Property As Integer
  24.     Get
  25.       return _quantity 
  26.     End Get
  27.     Set(ByVal value As Integer)
  28.       _quantity = value
  29.     End Set
  30.   End Property
  31.   Public Description Property As String
  32.     Get
  33.       return _description 
  34.     End Get
  35.     Set(ByVal value As String)
  36.       _description = value
  37.     End Set
  38.   End Property
  39.   Public Sub New()
  40.   End Sub
  41.   Public Sub New(ByVal name As String, ByVal price as Double, ByVal qty as Integer, ByVal desc as String)
  42.     _name = name
  43.     _price = price
  44.     _quantity = qty
  45.     _description = desc
  46.   End Sub
  47. End Class
(C#)
Expand|Select|Wrap|Line Numbers
  1. Public Class FoodItem
  2.   private String _name;
  3.   private Double _price;
  4.   private Integer _quantity;
  5.   private String _description; 
  6.  
  7.   public String Name{
  8.     get{ return _name;}
  9.     set{ _name = value;}
  10.   }
  11.   public Double Price{
  12.     get{return _price;}
  13.     set{_price = value;}
  14.   }
  15.   public Integer Quantity{
  16.     get{return _quantity;}
  17.     set{_quantity = value;}
  18.   }
  19.   public String Description{
  20.     get{return _description;}
  21.     set{_description = value;}
  22.   }
  23.   public FoodItem(){}
  24.   public FoodItem(String name,Double price,Integer qty, String desc)
  25.   {
  26.     _name = name;
  27.     _price = price;
  28.     _quantity = qty;
  29.     _description = desc;
  30.   }
  31. }
Now, create a "shopping cart" as "List<of FoodItem>s" and store that list in Session so that it is accessible on every page. Every time the user "adds an item to their cart" add a FoodItem object to the list of food items stored in session...


Like so:
(VB)
Expand|Select|Wrap|Line Numbers
  1. 'inside the "add to cart"
  2. 'first checking if the shopping cart... 
  3. 'the list List(Of FoodItem)...
  4. 'exists in session. 
  5. 'If it doesn't creating a new List(Of FoodItem)
  6. Dim shoppingCart as List(Of FoodItem)
  7. If Session("shoppingCart") Is Nothing Then
  8.   shoppingCart = new List(Of FoodItem)
  9. Else
  10.   shoppingCart = Ctype(Session("shoppingCart"), List(Of FoodItem))
  11. End If
  12.  
  13. 'Adding the FoodItem the user selected to the food cart
  14. Dim foodItemSelected As FoodItem = .....
  15. shoppingCart.Add(foodItemSelected)
  16.  
  17. 'Storing the shopping cart into Session:
  18.  
  19. Session("shoppingCart") = shoppingCart
(C#)
Expand|Select|Wrap|Line Numbers
  1. //inside the "add to cart"
  2. //first checking if the shopping cart... 
  3. //the list List<FoodItem>...
  4. //exists in session. 
  5. //If it doesn't creating a new List<FoodItem>
  6.  
  7. List<FoodItem> shoppingCart;
  8.  
  9. if(Session["shoppingCart"] == null)
  10. {
  11.   shoppingCart = new List<FoodItem>();
  12. }
  13. else
  14. {
  15.   shoppingCart = (List<FoodItem>)Session["shoppingCart"];
  16. }
  17.  
  18. //Adding the FoodItem the user selected to the food cart
  19. FoodItem foodItemSelected  = .....;
  20. shoppingCart.Add(foodItemSelected);
  21.  
  22. //Storing the shopping cart into Session:
  23.  
  24. Session["shoppingCart"] = shoppingCart;
Now on the "checkout page" you can retrieve the "shoppingCart" from Session, loop through all of the FoodItems and get their quantities and prices to determine how much to bill the user for....you can also use the "shoppingCart" stored in Session to display the information to the user...

-Frinny

6 1898
Frinavale
9,735 Expert Mod 8TB
You have to implement the "addToCart" button's Click event in your C# or VB.NET server-side code. In this method you will add the item to your "LinkedList".

-Frinny
Aug 17 '10 #2
Currently, I had added the value in the linked list. By the way, once I refresh my page, all the variable gone. Can I know hold to hold the value?
Aug 17 '10 #3
Frinavale
9,735 Expert Mod 8TB
ASP.NET works in a stateless environment. That means that all server side objects are created when the request is made....and when the response is sent to the browser all of the server side objects are destroyed.

There are a few techniques you could use to "persist" (remember/retain) the values though.

You could use Session: most commonly used when you need access to information in many pages in your website.

You could use ViewState: when you need to persist information on a single page.

You could also use cookies if you want to!

In the case of a Shopping Cart I would recommend using Session.

-Frinny
Aug 17 '10 #4
So far, I trying on cookies.
As I holding much data, thus... I still figuring out which is the better way for the implementation. Trying search on the session implementation way. You have any sample on that?

Eg.
If I add a food into shopping cart, it will hold few values such as food name and price. If I add second food, it will go into the list too.

Foodname: Fried rice Price: $4.00
Foodname: Noodles Price: $3.00
Foodname: Mushroom soup Price: $3.00
Aug 17 '10 #5
Frinavale
9,735 Expert Mod 8TB
I still think that Session is the best way to go.
It's pretty easy.

Create a "FoodItem" class that will contain information about the food being bought:
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class FoodItem
  2.   Private _name As String
  3.   Private _price As Double
  4.   Private _quantity as Integer
  5.   Private _description As String
  6.  
  7.   Public Name Property As String
  8.     Get
  9.      return _name 
  10.     End Get
  11.     Set(ByVal value As String)
  12.       _name = value
  13.     End Set
  14.   End Property
  15.  Public Price Property As Double
  16.     Get
  17.       return _price 
  18.     End Get
  19.     Set(ByVal value As Double)
  20.       _price = value
  21.     End Set
  22.   End Property
  23.   Public Quantity Property As Integer
  24.     Get
  25.       return _quantity 
  26.     End Get
  27.     Set(ByVal value As Integer)
  28.       _quantity = value
  29.     End Set
  30.   End Property
  31.   Public Description Property As String
  32.     Get
  33.       return _description 
  34.     End Get
  35.     Set(ByVal value As String)
  36.       _description = value
  37.     End Set
  38.   End Property
  39.   Public Sub New()
  40.   End Sub
  41.   Public Sub New(ByVal name As String, ByVal price as Double, ByVal qty as Integer, ByVal desc as String)
  42.     _name = name
  43.     _price = price
  44.     _quantity = qty
  45.     _description = desc
  46.   End Sub
  47. End Class
(C#)
Expand|Select|Wrap|Line Numbers
  1. Public Class FoodItem
  2.   private String _name;
  3.   private Double _price;
  4.   private Integer _quantity;
  5.   private String _description; 
  6.  
  7.   public String Name{
  8.     get{ return _name;}
  9.     set{ _name = value;}
  10.   }
  11.   public Double Price{
  12.     get{return _price;}
  13.     set{_price = value;}
  14.   }
  15.   public Integer Quantity{
  16.     get{return _quantity;}
  17.     set{_quantity = value;}
  18.   }
  19.   public String Description{
  20.     get{return _description;}
  21.     set{_description = value;}
  22.   }
  23.   public FoodItem(){}
  24.   public FoodItem(String name,Double price,Integer qty, String desc)
  25.   {
  26.     _name = name;
  27.     _price = price;
  28.     _quantity = qty;
  29.     _description = desc;
  30.   }
  31. }
Now, create a "shopping cart" as "List<of FoodItem>s" and store that list in Session so that it is accessible on every page. Every time the user "adds an item to their cart" add a FoodItem object to the list of food items stored in session...


Like so:
(VB)
Expand|Select|Wrap|Line Numbers
  1. 'inside the "add to cart"
  2. 'first checking if the shopping cart... 
  3. 'the list List(Of FoodItem)...
  4. 'exists in session. 
  5. 'If it doesn't creating a new List(Of FoodItem)
  6. Dim shoppingCart as List(Of FoodItem)
  7. If Session("shoppingCart") Is Nothing Then
  8.   shoppingCart = new List(Of FoodItem)
  9. Else
  10.   shoppingCart = Ctype(Session("shoppingCart"), List(Of FoodItem))
  11. End If
  12.  
  13. 'Adding the FoodItem the user selected to the food cart
  14. Dim foodItemSelected As FoodItem = .....
  15. shoppingCart.Add(foodItemSelected)
  16.  
  17. 'Storing the shopping cart into Session:
  18.  
  19. Session("shoppingCart") = shoppingCart
(C#)
Expand|Select|Wrap|Line Numbers
  1. //inside the "add to cart"
  2. //first checking if the shopping cart... 
  3. //the list List<FoodItem>...
  4. //exists in session. 
  5. //If it doesn't creating a new List<FoodItem>
  6.  
  7. List<FoodItem> shoppingCart;
  8.  
  9. if(Session["shoppingCart"] == null)
  10. {
  11.   shoppingCart = new List<FoodItem>();
  12. }
  13. else
  14. {
  15.   shoppingCart = (List<FoodItem>)Session["shoppingCart"];
  16. }
  17.  
  18. //Adding the FoodItem the user selected to the food cart
  19. FoodItem foodItemSelected  = .....;
  20. shoppingCart.Add(foodItemSelected);
  21.  
  22. //Storing the shopping cart into Session:
  23.  
  24. Session["shoppingCart"] = shoppingCart;
Now on the "checkout page" you can retrieve the "shoppingCart" from Session, loop through all of the FoodItems and get their quantities and prices to determine how much to bill the user for....you can also use the "shoppingCart" stored in Session to display the information to the user...

-Frinny
Aug 17 '10 #6
Hi Frinny,

Thanks lots. It solve my problem!!!
Easy to use compare cookies ;)
Aug 18 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
2
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of...
1
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
1
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID...
2
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic...
7
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
1
by: jecha | last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below <?...
15
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
3
by: Paulo | last post by:
Hi, beginner on asp.net 2.0 C# VS 2005, how can I use the shopping cart concept on my application? When the user clicks add item, it will be stored on some storage format, I dont know what is the...
1
by: BenKen | last post by:
Hi all pls I'm having great difficulty in making my shopping cart work. I am a newbie in php with little understanding of it. Add to cart button doesn't work and shopping cart in general doesn't. Pls...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.