240 likes | 281 Views
This project demonstrates the implementation of a shopping cart on a website using ASP.Net and a database. Users can search and view product information, add items to their cart, view cart contents, and check out. The shopping cart is modeled as a class with properties and methods to handle adding, deleting, viewing cart items, and checking out. An example database schema is provided with tables for customers, orders, order lines, products, and more. Specific steps for adding the shopping cart class to a website project are outlined. This project also includes event procedures for adding items to the cart, deleting items, and checking out.
E N D
Shopping Cart • Search and display product information • Add item to cart • View cart contents • Check out
Shopping Cart Example • Database: • WebCustomer: CustID, CustName, Addr, Phone • WebOrder: OrderID, CustID, OrderDate, CreditCardType, CreditCardNo • WebLine: OrderID, PID, Pname, Price, Qty • WebProduct: Pid, Pname, Price, UnitsInStock • OrderID • Login user using cookie • Each item is modeled as a class, and items are collected in a collection before added to the database.
Implementing Shopping Cart as Class • Shopping Cart Properties: • Public oid As String • Public cid As String • Public ODate As DateTime • Public CreditCardType As String • Public CreditCardNo as String • Public CollCartLine As New ArrayList() • Methods: AddItem, DeleteItem, ViewCart, CheckOut • DetailLine Properties: • OID • PID • Pname • Price • Qty • Amount = Qty * Price
Adding a Class to a Website • Website project: • Class files must save in App_Code folder • Right click Solution explorer and Choose Add ASP.Net Folder
ShopCart Class Public Class ShopCart Public oid As String Public cid As String Public ODate As DateTime Public CreditCardType As String Public CreditCardNo As String Public CollCartLine As New ArrayList() Public ReadOnly Property CartTotal() As Double Get Dim item As New CartLine Dim total As Double Dim itemIndex As Integer For Each item In CollCartLine total = total + item.price * item.qty Next CartTotal = total End Get End Property
AddItem Method Public Sub AddItem(ByVal line As CartLine) Dim item As New CartLine() Dim incart As New Boolean() incart = False If CollCartLine.Count > 0 Then For Each item In CollCartLine If item.pid = line.pid Then item.qty = line.qty incart = True Exit For End If Next End If If incart = False Then CollCartLine.Add(line) End If End Sub
Public Sub deleteItem(ByVal deleteIndex As Integer) CollCartLine.RemoveAt(deleteIndex) End Sub Public Sub checkOut() Dim objLine As New CartLine() Dim strSQL As String Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" Dim objConn As New OleDbConnection() objConn.ConnectionString = strConn Dim objComm As New OleDbCommand() objConn.Open() objComm.Connection = objConn objComm.CommandType = CommandType.Text strSQL = "insert into weborder values ('" & oid & "', '" & cid & "', #" & ODate.ToString & "#, '" & CreditCardType & "','" & CreditCardNo & "')" objComm.CommandText = strSQL objComm.ExecuteNonQuery() For Each objLine In CollCartLine strSQL = "insert into webline values ('" & objLine.oid & "', '" & objLine.pid & "', " & CStr(objLine.qty) & ")" objComm.CommandText = strSQL objComm.ExecuteNonQuery() Next End Sub End Class
Shopping Cart Detail Line Public Class CartLine Private hiddenoid As String Private hiddenpid As String Private hiddenpname As String Private hiddenprice As Double Private hiddenqty As Integer Public Property oid() As String Get oid = hiddenoid End Get Set(ByVal Value As String) hiddenoid = Value End Set End Property Public Property pid() As String Get pid = hiddenpid End Get Set(ByVal Value As String) hiddenpid = Value End Set End Property
Public Property pname() As String Get pname = hiddenpname End Get Set(ByVal Value As String) hiddenpname = Value End Set End Property Public Property price() As Double Get price = hiddenprice End Get Set(ByVal Value As Double) hiddenprice = Value End Set End Property Public Property qty() As Integer Get qty = hiddenqty End Get Set(ByVal Value As Integer) hiddenqty = Value End Set End Property Public ReadOnly Property amount() As Double Get amount = qty * price End Get End Property End Class
AddToCart Button Event Procedure Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged MyCart = Session("MyCart") Dim objLine As New CartLine() objLine.oid = Session.SessionID objLine.pid = GridView1.SelectedRow.Cells(0).Text objLine.pname = GridView1.SelectedRow.Cells(1).Text objLine.price = CDbl(GridView1.SelectedRow.Cells(2).Text) objLine.qty = CInt(CType(GridView1.SelectedRow.Cells(4).Controls(1), TextBox).Text) MyCart.AddItem(objLine) Response.Write("number of items in cart:" & CStr(MyCart.CollCartLine.Count)) Session("MyCart") = MyCart End Sub
Delete Button Procedure Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting MyCart.deleteItem(e.RowIndex) Session("MyShopCart") = MyCart GridView1.DataSource = MyCart.CollCartLine GridView1.DataBind() TextBox1.Text = MyCart.CartTotal.ToString End Sub
CheckOut Procedure Dim MyCart As New ShopCart() Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here MyCart = Session("MyShopCart") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MyCart.oid = Session.SessionID MyCart.cid = Session("cid") MyCart.ODate = DateTime.Now() MyCart.CreditCardType = RadioButtonList1.SelectedItem.Text MyCart.CreditCardNo = TextBox1.Text MyCart.checkOut() Response.Write("<p align='center'><font size='5'><b>Thank you for shopping at My.Com</b></font></p>") Session.Abandon() End Sub
Adding Buttons to a Bound GridView • Use GridView smart tag: • Edit Columns • Bound Fields, Command Fields, Template Fields • Example: • Drag a table from Server Explorer to create a bound gridView. • Use smart tag to choose Edit Columns • Add command field: • Edit • Update, Cancel • ASP.Net will execute the command automatically. • Delete • Select
Rearrange the Fields Order • Cancel the “Auto-Generate Fields” option • Then add bound fields: • For each bound field properties: • Appearance: • Header Text: This text will be used as column name • Data: • Data Field: for data binding • Arrange fields order
Adding Edit/Update/Canceland Delete Buttons • Use smart tag to choose Edit Columns • Add command field: • Edit • Update, Cancel • Delete
Adding Events for Edit/Update/Cance and Delete Buttons Dim objDataSet As New DataSet Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\salesDB.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String If Not Page.IsPostBack Then strSQL = "select * from customer;" Dim objAdapter As New OleDbDataAdapter(strSQL, objConn) objAdapter.Fill(objDataSet, "customer") GridView1.DataSource = objDataSet GridView1.DataMember = "customer" GridView1.DataBind() Session("MyDs") = objDataSet objConn.Close() Else objDataSet = Session("MyDs") End If End Sub Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit GridView1.EditIndex = -1 GridView1.DataSource = objDataSet GridView1.DataMember = "customer" GridView1.DataBind() End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting objDataSet.Tables("customer").Rows.RemoveAt(e.RowIndex) GridView1.DataSource = objDataSet GridView1.DataMember = "customer" GridView1.DataBind() End Sub Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing Response.Write(e.NewEditIndex) GridView1.EditIndex = e.NewEditIndex GridView1.DataSource = objDataSet GridView1.DataMember = "customer" GridView1.DataBind() End Sub Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating GridView1.EditIndex = -1 objDataSet.Tables("Customer").Rows(e.RowIndex).Item("Rating") = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text GridView1.DataSource = objDataSet GridView1.DataMember = "customer" GridView1.DataBind() End Sub
Adding Textbox and Button and to a Bound DataGrid • GridView smart tag/Edit Columns: • Template fields: • HeaderText: This text will be used as column name • From the design view, click GridView’s smart tag and select Edit Template • Drag a textbox to the template window.
Example <asp:TemplateColumn HeaderText="Quantity"> <ItemTemplate> <asp:TextBox id="txtQty" Width=70 runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="AddToCart"> <ItemTemplate> <asp:Button Text="AddToCart" Runat="server"></asp:Button> </ItemTemplate> </asp:TemplateColumn>
Command Field: Select • Select a row • Trigger events: • SelectedIndexChanged • Retrieve the selected row: • GridView1.SelectedRow • Retrieve cells of the selected row: • GridView1.SelectedRow.Cells(1).Text • Retrieve data entered in the textbox: • CType(GridView1.SelectedRow.Cells(4).Controls(1), TextBox).Text