。唯一标识符的转换错误,根本不应该被引用?

时间:2015-06-14 23:13:06

标签: sql vb.net

我正在开发一个网站的购物车,我有这个功能,可以从我的数据库中检索一张优惠券:

Public Shared Function CheckForVoucher(ByVal strVoucherName As String) As DataTable
    Dim connect As New SqlConnection

    Dim Data As New DataTable    ''Connection works, finds number of Vouchers in DB that match either code or ID. ID to be used for randomly generated vouchers.

    connect.ConnectionString = "SERVER = SERVER-SQL01; Trusted_Connection=yes; DATABASE=PCSQL"
    connect.Open()

    Dim query As String
    Dim search As String
    search = strVoucherName
    If search.Length >= 25 Then
        query = "SELECT * from PCSQL.dbo.X_WW_VOUCHER_DETAILS WHERE vID='" & search & "' "
    Else
        query = "SELECT * from PCSQL.dbo.X_WW_VOUCHER_DETAILS WHERE voucherName='" & search & "' "
    End If

    Dim command = New SqlDataAdapter(query, connect)
    command.Fill(Data)
    connect.Close()
    strVoucherName = query
    Return Data
End Function  

这一切都在我正在努力的网站上发挥作用。在客户订单结束时,它会触发另一个功能,将凭证的总使用量增加一个(我的老板希望确保人们不会将代码发送垃圾邮件并使他停止运营)。

Public Shared Function addUse(ByVal strVoucherName As String, intCurrentUses As Integer) As Boolean

    Dim connect As New SqlConnection
    Dim data As New DataTable
    connect.ConnectionString = "SERVER = SERVER-SQL01; Trusted_Connection = yes; DATABASE=PCSQL"
    connect.Open()
    Dim query As String
    Dim name As String = strVoucherName
    Dim current As Integer = intCurrentUses
    Dim newint As Integer = current + 1
    query = "UPDATE dbo.X_WW_VOUCHER_DETAILS SET currentUses= @currentUses WHERE voucherName=@voucherName"
    Dim command As New SqlCommand(query, connect)
    command.Parameters.Add("@voucherName", SqlDbType.VarChar)
    command.Parameters("@voucherName").Value = name
    command.Parameters.Add("@currentUses", SqlDbType.Int)
    command.Parameters("@currentUses").Value = newint

    command.ExecuteNonQuery()
    connect.Close()
    Return True
End Function  

这也可以正常工作,我可以检查SQL SERVER中的SQL表,看看“currentUses”列是否增加了1。耶。

问题

当我通过再次提交来测试代码时,如果如果 currentUses列等于validUses列,则会失败。我最终得出错误“从字符串转换为uniqueidentifier时转换失败”,即使根本没有直接引用唯一标识符。

我在一张有效5次的优惠券上进行了测试。时间1到4,没有错误,但是一旦currentUses也是5 ......不去。

为什么相等的列会导致我的函数自行崩溃?

SQL表

-vID    Uniqueidentifier
-voucherName    Nchar(15)
-expiryDate Date
-validUses  Int
-currentUses    Int
-discountType   Nchar(15)
-appliesTo  Nchar(15)
-numberOf   Nchar(20)
-Amount Int
-noOfItems  Int
-Category   Nchar(100)
-freebieID  Nchar(15)
-discountAmount Int
-Description    Nchar(255)

修改

我已将错误缩小到此if语句。

If search.Length >= 25 Then
    query = "SELECT * from PCSQL.dbo.X_WW_VOUCHER_DETAILS WHERE vID='" & search & "' "
Else
    query = "SELECT * from PCSQL.dbo.X_WW_VOUCHER_DETAILS WHERE voucherName='" & search & "' "
End If

使用

从cart.aspx调用凭证本身
    Protected Sub btnVoucherCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoucherCheck.Click

 Dim Data As DataTable = Voucher.CheckForVoucher(txtVoucher.text)
    If Data.Rows.Count > 0 Then
        Dim row As DataRow
        row = Data.Rows(0)



        Voucher.VoucherID = row.Item("vID").ToString().Trim()
        Voucher.VoucherName = row.Item("voucherName").ToString().Trim()
        Voucher.ExpiryDate = row.Item("ExpiryDate")
        Voucher.ValidUses = row.Item("ValidUses")
        Voucher.CurrentUses = row.Item("CurrentUses")
        Voucher.DiscountType = row.Item("DiscountType").ToString().Trim()
        Voucher.AppliesTo = row.Item("AppliesTo").ToString().Trim()
        Voucher.NumberOf = row.Item("NumberOf").ToString().Trim()
        Voucher.Amount = row.Item("Amount")
        Voucher.noOfItems = row.Item("NoOfItems")
        Voucher.Category = row.Item("Category").ToString().Trim()
        Voucher.FreebieID = row.Item("FreebieID").ToString().Trim()
        Voucher.DiscountAmount = row.Item("DiscountAmount")


        Dim count As Int32
        count = 0

        Dim expiry As DateTime = Voucher.ExpiryDate
        Dim today As DateTime = Date.Today()
        count = ((expiry - today).Days)


        If count <= -1 Then
            txtVoucher.Text = "Voucher expired"
        Else                'Further checks. One: Valid uses against current uses, checks if too many people have used the voucher.
            If (Voucher.CurrentUses >= Voucher.ValidUses) Then
                txtVoucher.Text = "Sorry, Voucher is no longer valid"
            Else 'Redirects to one of three methods based on discount type.

                c.VoucherName = Voucher.VoucherName

                If c.discounted = True Then
                    For Each item As RepeaterItem In rptCart.Items
                        Dim quantity As TextBox = CType(item.FindControl("txtQuantity"), TextBox)
                        Dim ProductID As HyperLink = CType(item.FindControl("hlProductID"), HyperLink)
                        Dim lblPrice As Label = CType(item.FindControl("lblPrice"), Label)
                        lblPrice.Text = Product.GetProductPrice(ProductID.Text, quantity.Text, c)

                    Next
                End If

                Select Case Voucher.DiscountType
                    Case "Dollar"
                        txtVoucher.Text = "$ Not yet Supported"
                    Case "Percentage"
                        percentageDiscount()
                    Case "Freebie"
                        txtVoucher.Text = "Freebie Not Yet Supported"
                    Case Else
                        txtVoucher.Text = "Not working."
                End Select
            End If
        End If
    Else


    End If
End Sub

在这个函数的中间比较了两个相关的列,但是如果它们等于与我的代码相同,那么我的代码就不会执行那么多。我得到转换错误引用我粘贴在上面的CheckForVoucher函数中的行。它不能是我的比较导致错误,因为我使用的DataTable在错误发生时并不完全存在。

1 个答案:

答案 0 :(得分:0)

您遇到的问题是当search的值为25个字符或更长时,您将其传递到数据库以与vID列进行比较。该列是uniqueidentifier,这意味着它是C#术语中的GUID。如果传入的值无法直接转换为GUID,则会出现转换错误。