查询在Sql中工作但返回空数据

时间:2015-06-02 23:21:11

标签: sql-server vb.net

修改

我很困惑,现在我改变了这句话:

 If Data.Rows.Count > 0 Then
 'Do all Voucher things in here
else
txtVoucher.text = "Sorry, Invalid Voucher"
end if

通过将凭证输出为凭证的字符串属性之一来显示凭证检查功能中的查询,如下所示:

else txtvoucher.text = Voucher.voucherName end if

一切正常!如果我将其更改回错误消息...数据表不返回任何行。我没有更改关于代码的任何其他内容,如果行数为0,那么文本框中的内容是什么。不管后面发送到文本框的内容,行数都不一样吗?

结束修改

我正在为网页制作一个基本的在线优惠券功能,并且我遇到了凭证检查查询的问题。

我有这个查询,它根据SQL表检查输入文本框的字符串是否匹配。

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 >= 20 Then
        query = "SELECT * from PCSQL.dbo.VOUCHER_DETAILS WHERE vID='" + search + "' "
    Else
        query = "SELECT * from PCSQL.dbo.VOUCHER_DETAILS WHERE voucherName='" + search + "' "
    End If

    Dim command = New SqlDataAdapter(query, connect)
    command.Fill(Data)
    connect.Close()

    Return Data
End Function

查询在SQL管理器中工作正常,我可以用列表中的任何凭证名称替换关键字搜索,并返回正确的结果,如果我替换vb.net中的搜索关键字并强制执行查询以检查特定凭证,无论在文本框中输入什么(例如TestVoucher2)。

我目前已设置页面以检查结果如下。

Protected Sub lbVoucherCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbVoucherCheck.Click

    'Voucher checking code to go here

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

        'Set voucher properties in Voucher.vb using datarow result.
        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")

        'lbVoucherCheck.Text = Data.ToString()


        'Step one: Check for Expiry Date
        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
            txtVoucher.Text = "Expires in " + count.ToString() + " days."
        End If
    Else
        txtVoucher.Text = Data.rows.count
    End If
End Sub

当我根据txtVoucher.Text输入运行查询时,它返回“0”,表示它没有找到任何内容。但是,如果我使用凭证名称来装配查询,则会返回正确的到期结果。

我强烈认为,它无法从txtVoucher.textVoucher.CheckForVoucher(txtVoucher.text)功能获取正确的信息。

0 个答案:

没有答案