Hi all…
I'm having this shopping cart problem…what I'm trying to do here, is that when a user click the Add To Cart button in the lunch_cart.aspx to add his desired lunch menu to his shopping cart in cafe_cart.aspx, the dish should be added into the shopping cart right? Let's say he adds Dish A, then he can see his Dish A in the shopping cart. But, he can't see the Quantity, Sub Total and Total in the cart! Moreover, let's say when he chooses to add another Dish B to the cart, the cart instead replaced the Dish A which had been added earlier! The only thing that my shopping cart can read/detect is the Dish ID, Dish Name and Price Per Set that are displayed correctly. Here's the relevant code for the lunch_cart.aspx:
Sub Page_Load(sender As Object, e As EventArgs)erm...i still can't solve the problem...the same thing happened when i modified a little. anyone can help me get on with this? i've been stuck with this coding for more than a month!Dim dt As DataTable
Dim nr As DataRow
Dim i As IntegerDim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("EzySys.mdb") & ";"
Dim Conn As New OLEDBConnection(strConn)
Dim strSQL As String = "SELECT * FROM dish_info WHERE Dish_ID='" & Request.Item("dishid") & "'"
Conn.Open()
Dim Cmd As New OLEDBCommand(strSQL,Conn)
Dim Dr As OLEDBDataReader=Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)if DR.read()
lbllunchdesc1.Text = DR("Dish_Desc").ToString()
lbllunchp1.Text = CStr(FormatCurrency(DR("Dish_Price").ToString()))
lbllunchp1.Text = RIGHT(lbllunchp1.Text,Len(lbllunchp1.Text)-1)
img1.imageurl = DR("Dish_Photo").ToString()
lblgrocery.text = DR("Dish_Name").ToString()
lblDishID.text = DR("Dish_ID").ToString()
end if
End SubSub AddToCart(sender As Object, e As EventArgs)
If txtQty.text="" Then
lblErrorMsg.text="Please enter a valid number!"
Else If NOT IsNumeric(txtQty.text) Then
lblErrorMsg.text="Please enter a valid number!"
Else If CInt(txtQty.text)<=0 OR CInt(txtQty.text)>10 Then
lblErrorMsg.text="Please enter a vaild number!"
Else If IsNumeric(txtQty.text) ThenDim conn As OleDbConnection
Dim strSelect As Stringconn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0; " & _
"Ole DB Services=-4; Data Source= " & server.mappath("EzySys.mdb"))strSelect = "SELECT * FROM dish_info WHERE Dish_ID='" & Request.Item("dishid") & "'"
conn.Open()
Dim Cmd As New OLEDBCommand(strSelect ,conn)
Cmd.ExecuteNonQuery()
conn.Close()Response.Redirect("cafe_cart.aspx?dishid=" & Request.Item("dishid"))
End If
End SubAnd here's the relevant code for cafe_cart.aspx:
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRowSub Page_Load(sender As Object, e As EventArgs)
'If Not Page.IsPostBack Then
makeCart()
'End If
End SubFunction makeCart()
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRowobjDT = New System.Data.DataTable("CafeCart")
objDT.Columns.Add("Dish ID", GetType(String))
objDT.Columns.Add("Dish Name", GetType(String))
objDT.Columns.Add("Price Per Set", GetType(Decimal))
objDT.Columns.Add("Quantity", GetType(Decimal))
objDT.Columns.Add("Sub Total", GetType(Decimal))Session("CafeCart") = objDT
Call AddDishes()
End Function
Function AddDishes()
Dim strConn As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("EzySys.mdb") & ";"
Dim Conn As New OLEDBConnection(strConn)
Dim strSQL As String = "SELECT * FROM dish_info WHERE Dish_ID='" & Request.Item("dishid") & "'"
Conn.Open()
Dim Cmd As New OLEDBCommand(strSQL,Conn)
Dim Dr As OLEDBDataReader=Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRowobjDT = Session("CafeCart")
Dim DishID As String
Dim DishName As String
Dim DishPrice As String
Dim DishQty As String
Dim DishSubT As StringIf Dr.Read()
DishID = Dr("Dish_ID").ToString()
DishName = Dr("Dish_Name").ToString()
DishPrice = CStr(FormatCurrency(Dr("Dish_Price").ToString()))
DishPrice = RIGHT(DishPrice,Len(DishPrice)-1)End If
DishQty = CDbl(Request.Item("txtQty"))
Dim qty As Double, price As Double
qty = CDbl(Request.Item("txtQty"))
price = CDbl(DishPrice)DishSubT = CDbl(qty * price)
Conn.Close()'this code is suppose to check whether a user has added the same item or not
'if he has added the same item again, then only the qty needs to be updated
Dim blnMatch As Boolean = FalseFor Each objDR In objDT.Rows
If objDR("Dish Name") = DishName Then
objDR("Quantity") += DishQty
blnMatch = True
Exit For
End If
Next'if the user has not added a new dish before, then a new row can be added
If Not blnMatch Then
objDR = objDT.NewRow
objDR("Dish ID") = DishID
objDR("Dish Name") = DishName
objDR("Price Per Set") = DishPrice
objDR("Quantity") = DishQty
objDR("Sub Total") = DishSubT
objDT.Rows.Add(objDR)
End If
Session("CafeCart") = objDTdgCafe.DataSource = objDT
dgCafe.DataBind()End Function
'this function is to calculate the subtotal (or is it grandtotal?)
Function GetItemTotal() As Decimal
Dim intCounter As Integer
Dim decRunningTotal As DecimalFor intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
decRunningTotal += (objDR("Price Per Set") * objDR("Quantity"))
NextReturn decRunningTotal
End Function'this code allows user to remove the dish from shopping cart
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)
objDT = Session("CafeCart")
objDT.Rows(e.Item.ItemIndex).Delete()
Session("CafeCart") = objDTdgCafe.DataSource = objDT
dgCafe.DataBind()lblTotal.Text = "RM" & GetItemTotal() 'recalculate the total after removing dish
End Sub
took a brief look at ur code, hasn't gone the full length. Well, for starter, u need to encode ur lunch quantity in ur url when u redirects to the cart.aspx....like -- Response.Redirect("cafe_cart.aspx?dishid=" & Request.Item("dishid") & "txtQty=" & txtQty.text) --
hi tantian
thanks a lot for your suggestion. i've tried your method and yes, it worked! but still... other problem persists...anyway, i'll try to modify and code again for a few more days. hopefully, i can solve the problem that's been bugging me for almost two months! ouch...
Suggestion: In your code, I see that your doing validation of the textboxes via if..else statements. I recommend to look at the validator controls. They are easy to use and you surely will benefit from them.
0 comments:
Post a Comment