Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
j.mendes24

Preencher Combo [Resolvido]

Recommended Posts

j.mendes24

bem pessoal tenho aqui uma duvida.

Como é que preencho uma combobox com os valores de uma tabela de uma base de dados criada em mysql.

:wallbash:

Share this post


Link to post
Share on other sites
j.mendes24

eu tenho este codigo.

Dim mysql As String = "SELECT ModeloCodMar FROM tabmodelos"
        Dim connection As New MySqlConnection(myconnectionstring)
        Dim command As New MySqlCommand(mysql, connection)

        connection.Open()

        Dim reader As MySqlDataReader = command.ExecuteReader

        While reader.Read()
            ModeloCodMar = reader.GetString(0)
            cbModMarca.Items.Add(ModeloCodMar)
        End While

mas este codigo não dá. alguma sugestão?  😡

Share this post


Link to post
Share on other sites
j.mendes24

ja consegui.

Dim myCommand As New MySqlCommand
        Dim myAdapter As New MySqlDataAdapter
        Dim myData As New DataTable
        Dim mySQL As String = "SELECT * FROM tabmarcas"
        Dim conn As MySqlConnection
        conn = New MySqlConnection
        conn.ConnectionString = (myconnectionstring)


        Try
            conn.Open()
            Try
                myCommand.Connection = conn
                myCommand.CommandText = mySQL
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)

                With cbModMarca
                    .DataSource = myData
                    .DisplayMember = "Marca"
                    .ValueMember = "Marca"
                    .SelectedIndex = 0
                End With

            Catch myerro As MySqlException
                MsgBox("Erro de leitura na Base de dados : " & myerro.Message)
            End Try
            conn.Close()
        Catch myerro As MySqlException
            MessageBox.Show("Erro ao conectar com a Base de dados : " & myerro.Message)
        Finally
            conn.Dispose()
        End Try

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.