如果使用SqlDataReader值的语句不起作用

时间:2018-04-03 15:40:37

标签: asp.net if-statement sqldatareader sqlcommand

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

对于" - "我正在做的按钮:

  1. 检查所选产品的CategoryID。
  2. 如果CategoryID = 3或4,则:
    • 检查所选产品的数量是否更多> 1。
    • 数量=数量 - 1.
    • 减少所选产品的HoursWork(用于预订插槽长度)。
    • 产品库存+ 1.
    • 减轻重量(用于交付价格)。
  3. 否则:标签显示"无法更改细节和细节数量代客泊位服务。
  4. 我使用SqlDataReader获取CategoryID,然后将其作为Integer存储在变量CategoryID中。

    我通过在标签中显示变量内容进行测试,并收集正确的CategoryID - 然而"如果CategoryID =" 3"或" 4"然后...... 不起作用,因为所有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。 缩减代码:

    受保护的子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,尺寸,价格,数量,小计,小时工作)

    产品 (产品ID,名称,标注,价格,尺寸,图片,缩略图,重量,LDescription,库存,类别ID,工作时间)

1 个答案:

答案 0 :(得分:0)

现在一切正常。

更改自:

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