从字符串“Edward”到“Integer”类型的转换无效

时间:2018-03-14 04:50:35

标签: visual-studio-2017

enter image description here

我是VB的新手。

我关注了视频,但这种情况一直在发生。

这是我的代码

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        con = New OleDbConnection
        con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Edward\Documents\AccessDb1.mdb;"
        Dim dr As OleDbDataReader
        Try
            con.Open()
            Dim Query As String
            Query = "SELECT * FROM tblok where pname='" & ComboBox2.Text & "'"
            cmd = New OleDbCommand(Query, con)
            dr = cmd.ExecuteReader
            While dr.Read

                Label2.Text = dr.GetString(dr("pname").ToString)


            End While
            con.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            con.Close()

        End Try


    End Sub

Imports System.Data.OleDb
Public Class Form2
    Dim conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Edward\Documents\AccessDb1.mdb;"
    Dim con As OleDbConnection = New OleDbConnection(conString)
    Dim cmd As OleDbCommand
    Dim adapter As OleDbDataAdapter
    Dim dt As DataTable = New DataTable()

 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        con.Open()
        Dim cm As New OleDbCommand("SELECT * FROM tblok WHERE pgen ='Male'", con)
        Dim dr As OleDbDataReader = cm.ExecuteReader
        While dr.Read
            ComboBox2.Items.Add(dr("pname").ToString)
        End While
        dr.Close()
        con.Close()

        con.Open()

        Dim cf As New OleDbCommand("SELECT * FROM tblok WHERE pgen ='Female'", con)
        Dim drf As OleDbDataReader = cf.ExecuteReader
        While drf.Read
            ComboBox1.Items.Add(drf("pname").ToString)
        End While
        drf.Close()
        con.Close()


    End Sub

  • 我的目标是在组合框中单击名称(链接到数据库) 数据库中的信息将显示在表单内的标签中。

还有!差点忘了〜

Label2.Text = dr.GetString(+1)似乎有用..但它只显示名称列

错误就在这里

 Label2.Text = dr.GetString(dr("pname").ToString)

我不知道。也许

提前致谢!

0 个答案:

没有答案