为什么我得到这种错误“System.MissingMemberException:'找不到类型'String'上的公共成员'Text'。'”在VB.Net中

时间:2017-07-22 12:43:47

标签: mysql vb.net

我正在尝试使用vb.net将数据插入MySql。当我尝试插入时,我得到了错误。请帮助我。

Private Sub addEmp_Click(sender As Object, e As EventArgs) Handles addEmp.Click

    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString =
   "server=localhost;userid=root;password=;database=vbdb"
    Dim READER As MySqlDataReader
    Dim birthDate As String = bDay.Value.ToString("yyyy/mm/dd")
    Dim dateHired As String = dHired.Value.ToString("yyyy/mm/dd")

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "insert into vbdb.employee_info (Last_Name, First_Name, Middle_Name, Gender, Birthdate, Address, Contact, Position, Emp_Type, Email, Date_Hired) values ('" & firstName.Text & "','" & lastName.Text & "','" & middleName.Text & "','" & gender.Text & "','" & birthDate & "','" & gender.Text & "','" & address.Text & "','" & contact.Text & "','" & position.SelectedItem.Text & "','" & type.SelectedItem.Text & "','" & email.Text & "','" & dateHired & "')"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        READER = Command.ExecuteReader

        MessageBox.Show("Data Saved")
        MysqlConn.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您正在使用position.SelectedItem.Text。据推测,positionComboBoxListBox,您已将其绑定到某个列表。据推测,您通过ValueMember指定的属性或列包含Strings。这意味着position.SelectedItemString,您正在尝试访问不存在的Text成员。

首先,你应该转Option Strict On。那个代码甚至不会编译。这意味着,如果position.SelectedItemString,则必须将其转换为该类型,可能使用CStr。如果您已经拥有所需的String并不是很明显,那么编译器会告诉您,而不必运行String类型没有Text属性的项目。