21
Shopping Cart Demo

Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

  • View
    227

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Shopping Cart Demo

Page 2: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Shopping Cart

• Search and display product information

• Add item to cart

• View cart contents

• Check out

• Note: WebsiteCart

Page 3: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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: Current time• Login user using cookie• Each item is modeled as a class, and items are

stored in a collection before added to the database.

Page 4: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 5: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 6: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

ShopCart ClassPublic 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

Page 7: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

AddItem MethodPublic 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

Page 8: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Public Sub deleteItem(ByVal deleteIndex As Integer)

CollCartLine.RemoveAt(deleteIndex)

End SubPublic 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

Page 9: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Shopping Cart Detail LinePublic 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

Page 10: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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 PropertyEnd Class

Page 11: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 12: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 13: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 14: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 15: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Adding Edit/Delete 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.– Click GridView’s smart tag and click:

• Enable Edit– Update, Cancel

– ASP.Net will execute the command automatically.

• Enable Delete

Page 16: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Rearrange the Fields Order

• Click GridView’s smart tag

• Select Edit Columns option

• Use the Up/Down button to rearrange fields order

Page 17: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Adding Textbox to a Bound DataGrid

• GridView smart tag/Edit Columns:– From the Available Fields list, select

Template fields and click Add:• HeaderText: This text will be used as column

name• Close this window

– From the design view, click GridView’s smart tag and select Edit Template

• Drag a textbox to the template window.• Click End Edit Template

Page 18: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Select Button

• Select button will trigger GridView’s SelectedIndexChange event. Use this event we can retrieve the value of every cell in the selected row.

• We can use this button to retrieve value entered in the Qty textbox.

• Properties:– GridView1.SelectedIndex– GridView1.SelectedRow.Cells(Index)

• 0-based index

Page 19: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

Adding a Select Button to a Bound GridView

• Click GridView’s smart tag and click:• Enable Select

• Click GridView smart tag and select Edit Columns option:– From the Selected Fields list, select Select

field and change its properties:• HeaderText: This text will be used as column

name• Select Text: This text will be displayed in every row

Page 20: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

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

Page 21: Shopping Cart Demo. Shopping Cart Search and display product information Add item to cart View cart contents Check out Note: WebsiteCart

GridView’s SelectedIndexChanged Event Procedure Example

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged Response.Write(GridView1.SelectedIndex) Response.Write(CType(GridView1.SelectedRow.Cells(4).Controls(1), TextBox).Text)

End Sub