使用来自MySQL的数据填充组合框

时间:2014-02-12 10:04:09

标签: mysql vb.net combobox dataset xampp

我正在尝试使用mysql中的表中的数据填充我的组合框,这是我的代码,我看不出是什么问题。我的组合框不会返回任何数据,只是空白。

    Private Sub cbxCompanyName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxCompanyName.SelectedIndexChanged
    Dim con As New MySqlConnection
    Dim constr As String = "Server=localhost;database=ba-solutions;user id=root;password="
    Try
        con.ConnectionString = constr
        con.Open()
    Catch ex As SqlException
        MsgBox(ex.Message)
    End Try

    Dim StrSql As String = "SELECT Company Name FROM client_details"
    Dim cmd As New MySqlCommand(StrSql, objconnection)
    Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
    Dim dt As New DataTable("Client_details")

    da.Fill(dt)


    If dt.Rows.Count > 0 Then

        cbxCompanyName.DataSource = dt
        cbxCompanyName.DisplayMember = "Company Name" 'What is displayed

    End If

End Sub

6 个答案:

答案 0 :(得分:1)

试试这个

If dt.Rows.Count > 0 Then

    cbxCompanyName.DataSource = dt
    cbxCompanyName.DisplayMember = "Company Name"
    cbxCompanyName.ValueMember = "Company Name"

End If

答案 1 :(得分:0)

试试这个..!

如果dt.Rows.Count> 0然后

    cbxCompanyName.DataSource = dt
    **cbxCompanyName.DataBind();**

End If

答案 2 :(得分:0)

你的sql命令文本错误。如果列名或表名中有空格,则应将名称括在一个特殊字符中,该字符用于分隔列名的开头和结尾。

Dim StrSql As String = "SELECT `Company Name` FROM client_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("Client_details")

对于MySql,这个特殊字符是反引号(按住 ALT 并在数字键盘上按 096 )。

当然,最好避开这些空间。
如果还不晚,我建议更改该列名,删除空格

答案 3 :(得分:0)

Private Sub cbxCompanyName_SelectedIndexChanged(sender As System.Object,e As System.EventArgs)处理cbxCompanyName.SelectedIndexChanged     Dim con As New MySqlConnection     Dim constr As String =“Server = localhost; database = ba-solutions; user id = root; password =”     尝试         con.ConnectionString = constr         con.Open()     Catch ex As SqlException         MSGBOX(ex.Message)     结束尝试

Dim StrSql As String = "SELECT Company Name FROM client_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)

Dim dr As OleDbDataReader = cmd.ExecuteReader

 While dr.Read
     cbxCompanyName.items.add(dr.item(0))
 End While
 dr.close()

End Sub

答案 4 :(得分:0)

当放入组合框时,以上所有内容都是正确的,但有一点是省略的。为了立即访问该列表,必须为所选项目设置起始索引,否则您将返回' System.Data.DataRowView'的内容。什么时候去收集ComboBox1.Text的内容。

da.SelectCommand = cmd
ds = New DataSet
Dim dt As New DataTable
da.Fill(ds, Table)
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "imgURL"
conn.Close()
da.Dispose()
conn.Dispose()
ComboBox1.SelectionStart = 0 'This is what is missing from above.
Dim current_imgURL As String = "http://10.1.1.10/photos/" & ComboBox1.Text
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New WebClient().DownloadData(current_imgURL)))

此示例加载带有图像文件名的组合框,并从服务器位置拉出它们。我想在从MySql填充后立即从组合框中预加载第一个图像并遇到这个问题,因为只有当用户点击组合框时才触发选择索引,而不是在加载数据时触发。

答案 5 :(得分:-1)

尝试此代码我希望它能够正常工作

Dim con As New MySqlConnection("connection string")
Dim da As New MySqlDataAdapter("SELECT * FROM tableName", con)
Dim dt As New DataTable

da.Fill(dt)
ComboBox1.DisplayMember = "fieldName to show in ComboBox"enter code here
ComboBox1.DataSource = dt
TextBox1.DataBindings.Add("Text", dt, "fieldName to show in textbox")