【问题标题】:If statement using SqlDataReader value not working如果使用 SqlDataReader 值的语句不起作用
【发布时间】:2018-09-13 00:21:33
【问题描述】:

我正在处理电子商务网站的购物车/购物车页面。具体来说:“-”链接按钮可将购物车中所选产品的数量减少1,然后“+”链接按钮可将购物车中所选产品的数量增加1。

对于我正在做的“-”按钮:

  1. 检查所选产品的 CategoryID。
  2. 如果 CategoryID = 3 或 4,则:
    • 检查所选产品的数量是否超过> 1。
    • 数量 = 数量 - 1。
    • 减少所选产品的 HoursWork 量(用于预订时段长度)。
    • 产品库存+1。
    • 减轻重量(用于交货价格)。
  3. 否则:标签显示“不能更改细节和代客服务的数量。

我正在使用 SqlDataReader 获取 CategoryID,然后将其作为 Integer 存储在变量 CategoryID 中。

我通过在标签中显示变量内容进行了测试,它收集了正确的 CategoryID - 但是 "If CategoryID = "3" or "4" Then... 不是工作,因为所有 categoryID 都在运行 IF 部分或语句,而不是 ELSE 部分。

    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

我也有 同样的问题,使用“+”链接按钮将购物车中所选产品的数量增加 1。 精简代码:

Protected Sub lIncrease_Click(ByVal sender As Object, ByVal e As EventArgs)

    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

两个表:

CART (CartID、UserID、DateCreated、ProductID、ProductName、Size、Price、Quantity、Subtotal、HoursWork)

产品 (ProductID、名称、SDescription、价格、尺寸、图像、缩略图、重量、LDescription、Stock、CategoryID、HoursWork)

【问题讨论】:

    标签: asp.net if-statement sqldatareader sqlcommand


    【解决方案1】:

    现在一切正常。

    更改自:

    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
    

    收件人:

     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 2013-06-20
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多