我在电子商务网站的购物车/篮子页面上工作。具体:" - "链接按钮以将所选产品的数量减少到购物车中,然后用" "链接按钮将所选产品的数量增加到购物车中。
对于我正在做的" - "按钮:
- 检查所选产品的类别。
- 如果类别= 3或4,则:
- 检查所选产品的数量是否更>1。
- 数量=数量-1。
- 减少所选产品的小时工作量(用于预订插槽长度(。
- 产品股票 1.
- 减轻体重(用于交付价格(。
- else:标签显示"无法更改数量以详细介绍&代客服务。
我正在使用sqldatareader获取categoryId,然后将其存储在变量类别中。
i通过在标签中显示变量内容来测试,并收集正确的categoryId -,但是"如果categoryId =" 3"或" 4",那么... 不是工作,因为所有类别都在运行IF部分或语句而不是其他部分。
Protected Sub lDecrease_Click(ByVal sender As Object, ByVal e As EventArgs)
'get clicked button
Dim lnk As LinkButton = CType(sender, LinkButton)
'Get clicked button row
' Dim row As GridViewRow = CType(lnk.Parent.Parent, GridViewRow)
Dim row As GridViewRow = CType(lnk.NamingContainer, GridViewRow)
'Get row selected
Dim idx As Integer = row.RowIndex
'get CartID
Dim lblCartID As Label = CType(row.Cells(0).FindControl("lblCartID"), Label)
Dim SCCartID As String = lblCartID.Text.ToString
'get ProductID
Dim lblProductID As Label = CType(row.Cells(0).FindControl("lblProductID"), Label)
Dim SCProductID As String = lblProductID.Text.ToString
'get ProductName
Dim lblProduct As Label = CType(row.Cells(0).FindControl("lblProduct"), Label)
Dim SCProduct As String = lblProduct.Text.ToString
'get Size
Dim lblSize As Label = CType(row.Cells(0).FindControl("lblSize"), Label)
Dim SCSize As String = lblSize.Text.ToString
'get Price
Dim lblPrice As Label = CType(row.Cells(0).FindControl("lblPrice"), Label)
Dim SCPrice As String = lblPrice.Text.ToString
'get Quantity
Dim lblQuantity As Label = CType(row.Cells(0).FindControl("lblQuantity"), Label)
Dim SCQuantity As String = lblQuantity.Text.ToString
'get Subtotal
Dim lblSubtotal As Label = CType(row.Cells(0).FindControl("lblSubtotal"), Label)
Dim SCSubtotal As String = lblSubtotal.Text.ToString
'get HoursWork
Dim lblHoursWork As Label = CType(row.Cells(0).FindControl("lblHoursWork"), Label)
Dim SCHoursWork As String = lblHoursWork.Text.ToString
'get Weight
Dim lblWeight As Label = CType(row.Cells(0).FindControl("lblWeight"), Label)
Dim SCWeight As String = lblWeight.Text.ToString
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
' start of category check
Dim cmd4 As SqlCommand = New SqlCommand
cmd4.CommandText = "SELECT products.CategoryID FROM products INNER JOIN Cart on products.ProductID = cart.ProductID WHERE cart.productID=@ProductID"
Dim ProductID2 As SqlParameter = New SqlParameter("@ProductID", SqlDbType.Int, 4)
ProductID2.Value = SCProductID
cmd4.Parameters.Add(ProductID2)
cmd4.Connection = conn
conn.Open()
cmd4.ExecuteNonQuery()
Dim reader As SqlDataReader = cmd4.ExecuteReader
Dim CategoryID As Integer
While reader.Read()
CategoryID = CType(reader.Item("CategoryID"), Integer)
End While
'Testing CategoryID value = success
lblNoStock.Visible = True
lblNoStock.Text = CategoryID
conn.Close()
'Nest IF statement based on Category ID 1,2, cannot be reduced in Quantity
If CategoryID = "3" Or "4" Then
' Run quantity check – And update if possible
Dim exists As Boolean = False
'cart quantity
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = "Select Quantity from Cart where CartID = @CartID"
cmd.Connection = conn
' conn.Close()
conn.Open()
'rename cart id 5
Dim CartID5 As SqlParameter = New SqlParameter("@CartID", SqlDbType.Int, 4)
CartID5.Value = SCCartID
cmd.Parameters.Add(CartID5)
'check if more than 1 in cart
exists = (CType(cmd.ExecuteScalar, Integer) > 1)
If exists Then
'show label to say no more in stock
lblNoStock.Visible = True
' lblNoStock.Text = "Available!"
conn.Close()
' update quantity & subtotal
Dim cmd1 As SqlCommand = New SqlCommand
cmd1.CommandText = "UPDATE Cart SET Quantity = Quantity - 1, Subtotal = @Subtotal - @Price WHERE CartID = @CartID"
'update hourswork
Dim cmd2 As SqlCommand = New SqlCommand
cmd2.CommandText = "UPDATE Cart SET HoursWork = (HoursWork - (Select Products.HoursWork FROM Products WHERE Products.ProductID = Cart.ProductID)) WHERE CartID = @CartID"
'UPDATE PRODUCTS STOCK + 1
'"UPDATE Products Set Stock = Stock + 1 WHERE ProductID = @ProductID"
Dim cmd3 As SqlCommand = New SqlCommand
'UPDATE weight
cmd3.CommandText = "UPDATE Cart SET Weight = (Weight - (Select Products.Weight FROM Products WHERE Products.ProductID = Cart.ProductID)) WHERE CartID = @CartID"
cmd1.Connection = conn
cmd2.Connection = conn
cmd3.Connection = conn
conn.Open()
Dim PProductID As SqlParameter = New SqlParameter("@ProductID", SqlDbType.Int, 4)
PProductID.Value = SCProductID
cmd1.Parameters.Add(PProductID)
Dim Subtotal As SqlParameter = New SqlParameter("@Subtotal", SqlDbType.Decimal, 5)
Subtotal.Value = SCSubtotal
cmd1.Parameters.Add(Subtotal)
Dim Price As SqlParameter = New SqlParameter("@Price", SqlDbType.Decimal, 5)
Price.Value = SCPrice
cmd1.Parameters.Add(Price)
Dim CartID As SqlParameter = New SqlParameter("@CartID", SqlDbType.Int, 4)
CartID.Value = SCCartID
cmd1.Parameters.Add(CartID)
Dim CartID2 As SqlParameter = New SqlParameter("@CartID", SqlDbType.Int, 4)
CartID2.Value = SCCartID
cmd2.Parameters.Add(CartID2)
Dim CartID3 As SqlParameter = New SqlParameter("@CartID", SqlDbType.Int, 4)
CartID3.Value = SCCartID
cmd3.Parameters.Add(CartID3)
Try
cmd1.ExecuteNonQuery()
cmd2.ExecuteNonQuery()
cmd3.ExecuteNonQuery()
cmd.ExecuteReader()
'show label to quantity updated
lblNoStock.Visible = True
' lblNoStock.Text = "Updated!"
Finally
conn.Close()
'Response.Redirect("Cart2.aspx")
End Try
Else
'show label to say no more in stock
lblNoStock.Visible = True
lblNoStock.Text = "Cannot reduce quantity: Remove from Cart!"
End If
' else
' lblNoStock.Visible = True
' lblNoStock.Text = "Cannot reduce quantity of Detailing/ Valeting services: Remove from Cart!"
Else
'Outer ELSE
'show label to say cannot change quantity
lblNoStock.Visible = True
lblNoStock.Text = "Quantity cannot be changed for Detailing and Valeting services!"
End If
End Sub
我还使用" "链接按钮的相同问题,以将所选产品的数量提高到购物车中。减少代码:
受保护的sub lincrease_click(byval发件人作为对象,byval e as evensargs(
Dim conn As SqlConnection = New SqlConnection(ConnectionString)
Dim exists As Boolean = False
'Check available in Products table STOCK
Dim cmd As SqlCommand = New SqlCommand
cmd.CommandText = "Select Stock from Products where ProductID = @ProductID"
cmd.Connection = conn
conn.Open()
Dim ProductID As SqlParameter = New SqlParameter("@ProductID", SqlDbType.Int, 4)
ProductID.Value = SCProductID
cmd.Parameters.Add(ProductID)
'check if more than 0 in stock
exists = (CType(cmd.ExecuteScalar, Integer) > 0)
If exists Then
conn.Close()
Dim cmd4 As SqlCommand = New SqlCommand
cmd4.CommandText = "SELECT products.CategoryID FROM products INNER JOIN Cart on products.ProductID = cart.ProductID WHERE cart.productID=@ProductID"
Dim ProductID2 As SqlParameter = New SqlParameter("@ProductID", SqlDbType.Int, 4)
ProductID2.Value = SCProductID
cmd4.Parameters.Add(ProductID2)
cmd4.Connection = conn
conn.Open()
cmd4.ExecuteNonQuery()
Dim reader As SqlDataReader = cmd4.ExecuteReader
Dim CategoryID As Integer
While reader.Read()
CategoryID = CType(reader.Item("CategoryID"), Integer)
End While
'Testing CategoryID value = success
' lblNoStock.Visible = True
'lblNoStock.Text = CategoryID
conn.Close()
'NESTED IF STATEMENT
If CategoryID = "3" Or "4" Then
' UPDATE cart QUANTITY & SUBTOTAL based on CartID
cmd1.CommandText = "UPDATE Cart SET Quantity = Quantity + 1, Subtotal = @Subtotal + @Price WHERE CartID = @CartID"
Dim cmd2 As SqlCommand = New SqlCommand
'UPDATE cart HOURSWORK
cmd2.CommandText = "/"
Dim cmd3 As SqlCommand = New SqlCommand
'UPDATE cart HOURSWORK
cmd3.CommandText = "/"
cmd1.Connection = conn
cmd2.Connection = conn
cmd3.Connection = conn
conn.Open()
//Declared Parameters
Try
cmd1.ExecuteNonQuery()
cmd2.ExecuteNonQuery()
cmd3.ExecuteNonQuery()
cmd.ExecuteReader()
Finally
conn.Close()
Response.Redirect("Cart2.aspx")
End Try
Else
'Nested ELSE
'show label to say cannot change quantity
lblNoStock.Visible = True
lblNoStock.Text = "Quantity cannot be changed for Detailing and Valeting services!"
End If
Else
'show label to say no more in stock
lblNoStock.Visible = True
lblNoStock.Text = "Sorry out of stock!"
End If
End Sub
两个表:
购物车(Cartid,userId,dateCreated,productid,productId,size,大小,价格,价格,数量,次数,小时工程(
products (生产力,名称,sdescription,价格,大小,图像,图像,缩略图,重量,ldescription,ldescription,cott,categoryId,hourswork(
现在都正常工作。
从:
更改Dim reader As SqlDataReader = cmd4.ExecuteReader
Dim CategoryID As Integer
While reader.Read()
CategoryID = CType(reader.Item("CategoryID"), Integer)
End While
conn.Close()
If CategoryID = "3" Or "4" Then
to:
Dim reader As SqlDataReader = cmd4.ExecuteReader
While reader.Read()
lblTest3.Text = CType(reader.Item("CategoryID"), Integer)
End While
conn.Close()
If lblTest3.Text.Contains("3") Or lblTest3.Text.Contains("4") Then