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.

Recommended Answers

All 5 Replies

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.

not to mention that you haven't set the connection string to anything.

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.