获取Databound组合框中所选项的值

时间:2018-02-07 13:40:21

标签: mysql vb.net combobox

我正在尝试获取vb中组合框的选定项的值以保存到数据库中但是它给了我以下错误

  

错误的整数值'System.Data.DataRowView'用于列类   ROW1

以下是我的代码的一部分

Dim Query As String
Query = "INSERT INTO smartaccounts.basic_fee (class, term, amount) VALUES(?, ?, ?)"
command = New MySqlCommand(Query, MysqlConn)
command.Parameters.Add(New MySqlParameter("class", CType(CmbClass.SelectedItem.ToString, String)))
command.Parameters.Add(New MySqlParameter("term", CType(CmbTerm.SelectedItem.ToString, String)))
command.Parameters.Add(New MySqlParameter("amount", CType(TxtAmount.Text, String)))

我对Vb很新,我想我一定做错了 它可能是什么,我该如何解决?

这就是我将数据传递给组合框

的方法
     Private Sub Fees_settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim adapter As New MySqlDataAdapter()
    Dim adapter1 As New MySqlDataAdapter
    Dim ds As New DataSet()
    Dim ds1 As New DataSet
    Dim i As Integer = 0
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server='" & My.Settings.MServer & "';user='" & My.Settings.MDbUser & "';password='" & My.Settings.MDbPass & "';database='" & My.Settings.MDb & "'"
    Dim sql As String = "SELECT name,label FROM classes "
    Dim sql1 As String = "SELECT name,label FROM term"
    Try
        MysqlConn.Open()
        command1 = New MySqlCommand(sql, MysqlConn)
        command2 = New MySqlCommand(sql1, MysqlConn)
        adapter.SelectCommand = command1
        adapter1.SelectCommand = command2
        adapter.Fill(ds)
        adapter1.Fill(ds1)
        adapter.Dispose()
        adapter1.Dispose()
        command1.Dispose()
        command2.Dispose()
        MysqlConn.Close()
        CmbClass.DataSource = ds.Tables(0)
        CmbClass.ValueMember = "label"
        CmbClass.DisplayMember = "name"
        CmbTerm.DataSource = ds1.Tables(0)
        CmbTerm.ValueMember = "label"
        CmbTerm.DisplayMember = "name"
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

`1,Policy,02/07/2018 11:57:39,Inbound Files,,False,` 替换为.SelectedItem.ToString
这是因为前者返回整个.SelectedValue.ToString,而后者仅返回绑定到所选项目的选定值。

您的代码现在应如下所示:

DataRow