我正在尝试使用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
答案 0 :(得分:0)
您正在使用position.SelectedItem.Text
。据推测,position
是ComboBox
或ListBox
,您已将其绑定到某个列表。据推测,您通过ValueMember
指定的属性或列包含Strings
。这意味着position.SelectedItem
是String
,您正在尝试访问不存在的Text
成员。
首先,你应该转Option Strict On
。那个代码甚至不会编译。这意味着,如果position.SelectedItem
是String
,则必须将其转换为该类型,可能使用CStr
。如果您已经拥有所需的String
并不是很明显,那么编译器会告诉您,而不必运行String
类型没有Text
属性的项目。