从组合框中获取值并插入它们

时间:2016-11-01 12:07:55

标签: mysql vb.net

我将记录存储到组合框中,ID为ValueMember,NAME为ViewMember,但当我通过从同一个组合框中获取ID将记录插入其他表时,它给出了一个错误,指出“无法更新或删除记录“,这导致记录没有插入。

这是我使用的组合框代码

Using CMD As New MySql.Data.MySqlClient.MySqlCommand
    With CMD
        .CommandText = "SELECT *FROM table1"
        .Connection = MySql.Data.MySqlClient.MySqlConnection
        .CommandType = CommandType.Text
    End With
    DA = New MySql.Data.MySqlClient.MySqlDataAdapter(CMD)
    ntbl1 = New DataTable("table1")
    DA.Fill(ntbl1)
    With cmbEmployee
        .DataSource = ntbl1
        .DisplayMember = "name_employee"
        .ValueMember = "id_employee"
    End With
End Using

以下是插入它们的代码

Using CMD As New MySql.Data.MySqlClient.MySqlCommand
    With CMD
        .CommandText = "INSERT INTO job VALUES ('NULL',@id_employee,@workarea)"
        .Connection = MySql.Data.MySqlClient.MySqlConnection
        .CommandType = CommandType.Text
        .Parameters.AddWithValue("@id_employee", cmbEmployee.ValueMember)
        .Parameters.AddWithValue("@workarea", cmbWorkarea.Text)
        If MsgBox("Proceed?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            .ExecuteNonQuery()
        End If
    End With
End Using

如何避免此错误,如果可以,请提供代码示例?

编辑: 使用属性Combobox.SelectedValue可以正常工作。

0 个答案:

没有答案