VB.Net and MySQL with Arrays?

Joined
Dec 10, 2006
Messages
540
I am using MySQL's Connector/Net 5.0 in Visual Studio 2005 with Visual Basic to have my Visual Basic application read from a MySQL database. Now one problem I am running into is I have a dropdown/combobox that I want populated with the name field from table Users. So I need to know how to have the command ComboBox1.Items.Add(varName),where varName is the Name field from the table, and I would like that command ran for each listing with each Name. Anyone got any ideas how to do this?
 
Are you having problems getting the data out of the database or getting it into your application?
 
I only know how to get single results out of MySQL and use it in Visual Basic, so basically I need help on both aspects in terms to gathering a unknown amount of results.
 
I figured it out, and I can't explain it so I will let the code do the talking if anyone else needs to know how to do this.

Code:
    Dim conn As MySqlConnection
    Dim data As DataTable
    Dim da As MySqlDataAdapter
    Dim cb As MySqlCommandBuilder
    Dim connStr As String
        connStr = String.Format("server={0};user id={1}; password={2}; database=phone; pooling=false", _
    varServer, varUser, varPass)

        conn = New MySqlConnection(connStr)
            conn.Open()
            Dim reader As MySqlDataReader
            reader = Nothing
        Dim cmd As New MySqlCommand("SELECT ext,caller_id FROM extensions", conn)
            Try
                reader = cmd.ExecuteReader()
                ComboBox1.Items.Clear()

            While (reader.Read())
                ComboBox1.Items.Add(reader.GetString(1) + " (" + reader.GetString(0) + ")")
            End While
            Catch ex As MySqlException
                MessageBox.Show("Failed to populate database list: " + ex.Message)
            Finally
                If Not reader Is Nothing Then reader.Close()
            End Try
 
You are also going to want to close that connection in your finally block.
 
Subtle dig at the fact he's using VB.Net? :rolleyes: :D

I'm confused. I'm not a VB.NET Developer, but looking around seems to suggest that they added using blocks in VB.NET 2.0, and if he's using Visual Studio 2005 I can only assume that he's using .NET 2.0. I might have missed what your comment implied, though.
 
Back
Top