Results 1 to 7 of 7

Thread: [VB.NET] Fill combobox from MySQL!

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    47

    [VB.NET] Fill combobox from MySQL!

    Hey guys!

    I've got a combobox, and I need to fill it up with data from a column in a MySQL table. When one of the cells is selected, the data in the cell in the next row(on the right) is displayed in a textbox. I know how to create a connection, but I can't figure out how to do the rest. I've searched on internet but without brilliant results. Assuming I the table is like this:

    Name|Url
    Hello | hello.com
    BLA | bla.com

    Thanks in advance?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [VB.NET] Fill combobox from MySQL!

    use a dataadapter to fill a datatable + set that datatable as your combobox datasource, + the field name as the combobox displaymember

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    47

    Re: [VB.NET] Fill combobox from MySQL!

    Thanks. How do I fill a datatable though?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [VB.NET] Fill combobox from MySQL!

    what do you have so far?
    post your code + i'll show you how to modify it

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [VB.NET] Fill combobox from MySQL!

    ok. here's how then:

    Code:
    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"
    ComboBox1.DataSource = dt
    TextBox1.DataBindings.Add("Text", dt, "fieldName to show in textbox")

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    47

    Re: [VB.NET] Fill combobox from MySQL!

    Quote Originally Posted by .paul. View Post
    ok. here's how then:

    Code:
    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"
    ComboBox1.DataSource = dt
    TextBox1.DataBindings.Add("Text", dt, "fieldName to show in textbox")
    Thanks! For this code, how would the connection string be?
    I got this, Dim con As New MySqlConnection("server=localhost; user id=root; password=; database=test")

    Is it ok?
    Last edited by aristide; May 8th, 2013 at 09:03 AM.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [VB.NET] Fill combobox from MySQL!

    have a look at www.connectionstrings.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width