为什么单击与其事件关联的按钮时组合框会引发错误



程序的基本概要:连接到数据库。组合框中填充了一个术语列表,选择术语后,按下"获取术语"按钮将填充列表视图,然后在窗体底部的文本框中返回到期总余额。

组合框填充,但是当按下 btn 时,catch ex 会抛出沿字符串行的错误无法转换为整数(发生在表单设计代码上)。我不太确定我哪里出错了。它似乎在任何地方都没有抓住其他任何东西。

我将包含下面的代码

Imports Payables

公开课表格1

Dim invoiceList As List(Of Invoices)
Dim termList As List(Of Terms)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    LoadComboBoxes()
End Sub
Private Sub LoadComboBoxes()
    termList = TermsDB.GetTermsList
    cboTerms.DataSource = termList
    cboTerms.ValueMember = "TermsID"
    cboTerms.DisplayMember = "Description"
End Sub

Private Sub btnGetInvoice_Click(sender As Object, e As EventArgs) Handles btnGetInvoice.Click
    Dim invoiceList As List(Of Invoices)
    Dim TermsID = CInt(cboTerms.SelectedValue)

    Try
        invoiceList = InvoicesDB.FindInvoiceByID(TermsID)
        txtTotalBalanceDue.Text = FormatCurrency(InvoicesDB.GetBalanceDue())
        If invoiceList.Count > 0 Then
            Dim invoice As Invoices
            For i = 0 To invoiceList.Count - 1
                invoice = invoiceList(i)
                lvInvoices.Items.Add(invoice.InvoiceID)
                lvInvoices.Items(i).SubItems.Add(invoice.VendorID)
                lvInvoices.Items(i).SubItems.Add(invoice.InvoiceNumber)
                lvInvoices.Items(i).SubItems.Add(invoice.InvoiceDate)
                lvInvoices.Items(i).SubItems.Add(invoice.InvoiceTotal)
                lvInvoices.Items(i).SubItems.Add(invoice.PaymentTotal)
                lvInvoices.Items(i).SubItems.Add(invoice.CreditTotal)
                lvInvoices.Items(i).SubItems.Add(invoice.TermsID)
                lvInvoices.Items(i).SubItems.Add(invoice.DueDate)
                lvInvoices.Items(i).SubItems.Add(invoice.PaymentDate)
            Next
        Else
            MessageBox.Show("There is no info on this account")
            Me.Close()
        End If
    Catch ex As Exception
        Throw ex
        Me.Close()
    End Try

End Sub


Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

结束类

Public Class Invoices
Dim m_InvoiceID As Integer
Dim m_VendorID As Integer
Dim m_InvoiceNumber As String
Dim m_InvoiceDate As Date
Dim m_InvoiceTotal As Decimal
Dim m_PaymentTotal As Decimal
Dim m_CreditTotal As Decimal
Dim m_TermsID As Integer
Dim m_DueDate As Date
Dim m_PaymentDate As Date

Public Sub New()
End Sub
Public Property InvoiceID() As Integer
    Get
        Return m_InvoiceID
    End Get
    Set(value As Integer)
        m_InvoiceID = value
    End Set
End Property
Public Property VendorID() As Integer
    Get
        Return m_VendorID
    End Get
    Set(value As Integer)
        m_VendorID = value
    End Set
End Property
Public Property InvoiceNumber() As String
    Get
        Return m_InvoiceNumber
    End Get
    Set(value As String)
        m_InvoiceNumber = value
    End Set
End Property
Public Property InvoiceDate() As Date
    Get
        Return m_InvoiceDate
    End Get
    Set(value As Date)
        m_InvoiceDate = value
    End Set
End Property
Public Property InvoiceTotal() As Decimal
    Get
        Return m_InvoiceTotal
    End Get
    Set(value As Decimal)
        m_InvoiceTotal = value
    End Set
End Property
Public Property PaymentTotal() As Integer
    Get
        Return m_PaymentTotal
    End Get
    Set(value As Integer)
        m_PaymentTotal = value
    End Set
End Property
Public Property CreditTotal() As Integer
    Get
        Return m_CreditTotal
    End Get
    Set(value As Integer)
        m_CreditTotal = value
    End Set
End Property
Public Property TermsID() As Integer
    Get
        Return m_TermsID
    End Get
    Set(value As Integer)
        m_TermsID = value
    End Set
End Property

Public Property DueDate() As Date
    Get
        Return m_DueDate
    End Get
    Set(value As Date)
        m_DueDate = value
    End Set
End Property
Public Property PaymentDate() As Date
    Get
        Return m_PaymentDate
    End Get
    Set(value As Date)
        m_PaymentDate = value
    End Set
End Property


'Create a function BalanceDue to return the BalanceDue
Public Function GetBalanceDue() As Decimal
    Return m_InvoiceTotal - m_PaymentTotal - m_CreditTotal
End Function

结束类

Imports System.Data.SqlClient

公共类发票数据库

Public Shared Function FindInvoiceByID(ByVal TermsID) As List(Of Invoices)
    Dim invoice As New Invoices
    Dim connection As SqlConnection = PayablesDB.GetConnection
    Dim invoiceList As New List(Of Invoices)
    Dim selectStatement As String = "SELECT InvoiceID, VendorID, InvoiceNumber, InvoiceDate, InvoiceTotal,PaymentTotal,CreditTotal,TermsID,DueDate,PaymentDate FROM Invoices WHERE TermsID=@TermsID"
    Dim selectCommand As New SqlCommand(selectStatement, connection)
    'add the parameter to the parameter collection of the command object
    selectCommand.Parameters.AddWithValue("@TermsID", TermsID)
    Try
        connection.Open()
        Dim reader As SqlDataReader = selectCommand.ExecuteReader
        If reader.Read Then
            invoice.InvoiceID = CInt(reader("InvoiceID"))
            invoice.VendorID = CInt(reader("VendorID"))
            invoice.InvoiceNumber = CInt(reader("InvoiceNumber"))
            invoice.InvoiceDate = CDate(reader("InvoiceDate"))
            invoice.InvoiceTotal = CDec(reader("InvoiceTotal"))
            invoice.PaymentTotal = CDec(reader("PaymentTotal"))
            invoice.CreditTotal = CDec(reader("CreditTotal"))
            invoice.TermsID = CInt(reader("TermsID"))
            invoice.DueDate = CDate(reader("DueDate"))
            invoice.PaymentDate = CDate(reader("PaymentDate"))
        Else
            'that means the invoice is not found
            invoice = Nothing 'this means the vendor object no longer exists
        End If
        reader.Close()
        connection.Close()
    Catch ex As Exception
        Throw ex
    End Try
    Return invoiceList
End Function
Public Shared Function GetBalanceDue() As Decimal 'aggregate 
    Dim connection As SqlConnection = PayablesDB.GetConnection
    Dim selectCommand As New SqlCommand()
    selectCommand.Connection = connection
    selectCommand.CommandText =
        "SELECT SUM(InvoiceTotal - PaymentTotal - CreditTotal) " &
        "AS BalanceDue FROM Invoices" &
        "WHERE TermsID=@TermsID"
    connection.Open()
    Dim balanceDue As Decimal = CDec(selectCommand.ExecuteScalar)  
    connection.Close() 
    Return balanceDue
End Function

结束类

Public Class Terms
Dim m_TermsID As Integer
Dim m_Description As String
Dim m_DueDays As Integer
Public Sub New()
End Sub
Public Property TermsID() As Integer
    Get
        Return m_TermsID
    End Get
    Set(ByVal value As Integer)
        m_TermsID = value
    End Set
End Property
Public Property Description() As String
    Get
        Return m_Description
    End Get
    Set(ByVal value As String)
        m_Description = value
    End Set
End Property
Public Property DueDays() As Integer
    Get
        Return m_DueDays
    End Get
    Set(ByVal value As Integer)
        m_DueDays = value
    End Set
End Property

结束类

Imports System.Data.SqlClient

公共类条款数据库

Public Shared Function GetTermsList() As List(Of Terms)
    Dim termList As New List(Of Terms)
    Dim connection As SqlConnection = PayablesDB.GetConnection
    Dim selectStatement As String =
        "SELECT TermsID,Description,DueDays " &
        "FROM Terms " &
        "ORDER BY Description"
    Dim selectCommand As New SqlCommand(selectStatement, connection)
    Try
        connection.Open()
        Dim reader As SqlDataReader = selectCommand.ExecuteReader()
        Dim term As Terms
        Do While reader.Read
            term = New Terms
            term.TermsID = CInt(reader("TermsID"))
            term.Description = reader("Description").ToString
            term.DueDays = CInt(reader("DueDays"))
            termList.Add(term)
        Loop
        reader.Close()
    Catch ex As SqlException
        Throw ex
    Finally
        connection.Close()
    End Try
    Return termList
End Function

结束类

我认为您的按钮正在触发页面加载事件,并且您正在丢失选定的组合框值。

您是否将文本值作为组合框中的第一个选项?这会导致您看到的转换错误。

避免在回发时使用类似内容绑定组合框

If ispostback = false Then
LoadComboBoxes()
End If

最新更新