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

Webboy2008Asked:
Who is Participating?
 
Amandeep Singh BhullarConnect With a Mentor Commented:
Since this is your Home work, so you should do it your self.

I have some suggestions for you:
1. Change the Database table structure. Added the UserID to it, to identify who ordered the products
2. To insert the DataTable into DB, loop throug it and insert data
3. Create a HTML structure and mail it to user. Check:
     http://www.codeproject.com/KB/aspnet/EmailApplication.aspx
0
 
Webboy2008Author Commented:
I never said this is my home work? Are u ok?
0
 
Amandeep Singh BhullarCommented:
Apologies
Have you tried my suggestions
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.