Visual Basic错误类型和数据不匹配的异常

时间:2015-02-19 15:29:13

标签: c# vb.net

我正在尝试将组合框填充到MS Access数据库中并将值显示到文本框中,组合框可以将ID读入数据库但是当我选择将数据显示到文本框中时我会遇到此错误

"类型' System.Data.OleDb.OleDbException'的第一次机会异常。发生在System.Data.dll

其他信息:标准表达式中的数据类型不匹配。"

到目前为止,这是我的代码。

Public Class List_of_patient
    Sub fillcombo()
        strsql = "select * from ptInfo"
        Dim acscmd As New OleDb.OleDbCommand
        acscmd.CommandText = strsql
        acscmd.Connection = acsconn
        acsdr = acscmd.ExecuteReader
        While (acsdr.Read())
            ComboBox1.Items.Add(acsdr("PaintNo"))
        End While

        acscmd.Dispose()
        acsdr.Close()

    End Sub

    Private Sub List_of_patient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Module1.connect()
        Me.fillcombo()

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        strsql = "select * from ptInfo where PaintNo = '" & ComboBox1.Text & "'"
        Dim acscmd As New OleDb.OleDbCommand
        acscmd.CommandText = strsql
        acscmd.Connection = acsconn
        acsdr = acscmd.ExecuteReader

        If (acsdr.Read() = True) Then
            TextBox4.Text = (acsdr("diag"))
            TextBox3.Text = (acsdr("Pname"))

        End If
        acscmd.Dispose()
        acsdr.Close()

    End Sub



End Class

这是我的模块

Imports System.Data.OleDb
Module Module1
    Public acsconn As New OleDb.OleDbConnection
    Public acsdr As OleDbDataReader
    Public strsql As String

    Sub connect()
        acsconn.ConnectionString = "Provider=Microsoft.ace.oledb.12.0; data source=|datadirectory|patient.accdb"
        acsconn.Open()
    End Sub

End Module

请帮帮我。提前谢谢:)

1 个答案:

答案 0 :(得分:0)

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
                strsql = "select * from ptInfo where PaintNo = " & CLng(ComboBox1.Text) 
                Dim acscmd As New OleDb.OleDbCommand
                acscmd.CommandText = strsql
                acscmd.Connection = acsconn
                acsdr = acscmd.ExecuteReader

                If (acsdr.Read() = True) Then
                    TextBox4.Text = (acsdr("diag"))
                    TextBox3.Text = (acsdr("Pname"))

                End If
                acscmd.Dispose()
                acsdr.Close()

            End Sub