Link to home
Create AccountLog in
Avatar of Webboy2008
Webboy2008

asked on

asp.net c# shopping cart module

I have the code attached. It is my shopping cart (1 page) that I tried to get my small project done.
I would like to add Checkout button.

When it is clicked, it will do the following:

1. Add all of the selected products (qty, productname, description) into a database table (sql server). The table structure is exact same as datatable.

2. Email to someone with the selected data.

Thanks,
<%@ Page debug=true Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<html>
<head>
<title>Shopping Cart</title>
<script runat="server">
Dim objDT As System.Data.DataTable 
Dim objDR As System.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
     If Not IsPostBack Then
          makeCart()
     End If
End Sub

    Function makeCart()
        
        objDT = New System.Data.DataTable("Cart")
        objDT.Columns.Add("ID", GetType(Integer))
        objDT.Columns("ID").AutoIncrement = True
        objDT.Columns("ID").AutoIncrementSeed = 1

        objDT.Columns.Add("Quantity", GetType(Integer))
        objDT.Columns.Add("Product", GetType(String))
        objDT.Columns.Add("Cost", GetType(Decimal))
 
        Session("Cart") = objDT
    End Function

Sub AddToCart(s As Object, e As EventArgs)
	objDT = Session("Cart")
	Dim Product = ddlProducts.SelectedItem.Text
	Dim blnMatch As Boolean = False

	For Each objDR In objDT.Rows
		If objDR("Product") = Product Then
			objDR("Quantity") += txtQuantity.Text
			blnMatch = True
			Exit For
		End If
	Next

	If Not blnMatch Then
	objDR = objDT.NewRow
	objDR("Quantity") = txtQuantity.Text
	objDR("Product") = ddlProducts.SelectedItem.Text
	objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
	objDT.Rows.Add(objDR)
	End If
	Session("Cart") = objDT

	dg.DataSource = objDT
	dg.DataBind()
	
	lblTotal.Text = "$" & GetItemTotal()
End Sub

Function GetItemTotal() As Decimal
	Dim intCounter As Integer
	Dim decRunningTotal As Decimal
	
	For intCounter = 0 To objDT.Rows.Count - 1
		objDR = objDT.Rows(intCounter)
		decRunningTotal += (objDR("Cost") * objDR("Quantity"))
	Next
	
	Return decRunningTotal 
End Function

Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
     objDT = Session("Cart")
     objDT.Rows(e.Item.ItemIndex).Delete()
     Session("Cart") = objDT

     dg.DataSource = objDT
     dg.DataBind()

     lblTotal.Text = "$" & GetItemTotal()
End Sub

    
</script>

</head>
<body>
<form runat="server">
Product:<br>
<asp:DropDownList id=ddlProducts runat="server">
<asp:ListItem Value="4.99">Socks</asp:ListItem>
<asp:ListItem Value="34.99">Pants</asp:ListItem>
<asp:ListItem Value="14.99">Shirt</asp:ListItem>
<asp:ListItem Value="12.99">Hat</asp:ListItem>
</asp:DropDownList><br>
Quantity:<br>
<asp:textbox id="txtQuantity" runat="server" /><br><br>
<asp:Button id=btnAdd runat="server" Text="Add To Cart" onClick="AddToCart" /><br><br>
<asp:DataGrid id=dg runat="server" ondeletecommand="Delete_Item">
<columns>
<asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove Item" />
</columns>
</asp:DataGrid>
<br><br>
Total: 
<asp:Label id=lblTotal runat="server" /><br /> 
</form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Amandeep Singh Bhullar
Amandeep Singh Bhullar
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Webboy2008
Webboy2008

ASKER

I never said this is my home work? Are u ok?
Apologies
Have you tried my suggestions