Can someone tell me what's wrong here:

Dim sql As String = "SELECT * FROM courses"
        Dim con As New MySqlConnection
        Dim dt As New DataTable(sql)
        Dim cmd As New MySqlCommand(sql, con)

        con.Open()

        ComboBox1.Items.Add(dt)

It doesn't show the courses.

You aren't accessing the database correctly. You normally use a dataAdapter to fill the dataTable.

Dim da As new MySqlDataAdapter(cmd) // pass in your command object here
da.Fill(dt) // automatically open connection, extract data to dt and close connection

Now you have a dataTable with your data in it.
ComboxBox1.Items.Add(dt) won't work either. You can loop through the dt adding each item or link the dt as the ComboBox1's data source.

hello PF2G !
use this code to fill combobox

dim myCon as new sqlconnection("ConnectionString")
myCon.open()
dim da as new sqldatadapter("yourquery_select username from users",myCOn)
dim dt as datatable
da.fill(dt)
combobox.datasource = dt
combobox.displaymember = "username" ' your column name ,which you want to show

hope this will helps you
Regards
M.Waqas Aslam

Can someone tell me what's wrong here:

Dim sql As String = "SELECT * FROM courses"
        Dim con As New MySqlConnection
        Dim dt As New DataTable(sql)
        Dim cmd As New MySqlCommand(sql, con)

        con.Open()

        ComboBox1.Items.Add(dt)

It doesn't show the courses.

well, you can do that with adapter or reader.. make a choice.. if you want to do that with adapter, just take a look waqasaslammmeo code, but if you want to do that with reader, just take a look this :

try
conn = New MySqlConnection("server=localhost;user=your_mysql_user;password=your_mysql_password;database=your_database_name")

            conn.Open()

            Dim command As New MySqlCommand
            Dim sqlquery As String = "SELECT * FROM course"
            Dim adapter As New MySqlDataAdapter
            Dim data As MySqlDataReader

            command.CommandText = sqlquery
            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader

            While data.Read()
                ComboBox1.Items.Add(data("name_of_course").ToString)
            End While

            data.Close()
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

feel free to choice.. :D

Edited 4 Years Ago by artemix22: n/a

You aren't accessing the database correctly. You normally use a dataAdapter to fill the dataTable.

Dim da As new MySqlDataAdapter(cmd) // pass in your command object here
da.Fill(dt) // automatically open connection, extract data to dt and close connection

Now you have a dataTable with your data in it.
ComboxBox1.Items.Add(dt) won't work either. You can loop through the dt adding each item or link the dt as the ComboBox1's data source.

if you use dt as datasource then the possible code would be combobox1.datasource(dt), am I correct?

This question has already been answered. Start a new discussion instead.