1 / 23

Shopping Cart Demo

Shopping Cart Demo. 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

glennisg
Download Presentation

Shopping Cart Demo

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shopping Cart Demo

  2. Shopping Cart • Search and display product information • Add item to cart • View cart contents • Check out

  3. 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.

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Working with GridView ControlMurach’s ASP.Net Chap. 14

  15. 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

  16. Adding Command Fields to a Bound GridView Created by Code

  17. 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

  18. Adding Edit/Update/Canceland Delete Buttons • Use smart tag to choose Edit Columns • Add command field: • Edit • Update, Cancel • Delete

  19. 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

  20. 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

  21. 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.

  22. 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>

  23. 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

More Related