How to fill combobox with SQL query? W#

0

I would like to know if it is possible to pass data from an SQL query to a combobox using C #. I researched the subject and found examples, but when I adapted to my case, I could not use it. I'm doing it this way:

private void carregacombo()
{
    Conexao conexão = new Conexao();
    try
    {
        conexão.conectar();
        OleDbCommand cmd = new OleDbCommand("select nomeEquipe from Equipe",conexão.cn);
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable dtMensagens = new DataTable();
        da.Fill(dtMensagens);
        this.cmbEquipe.DataSource = dtMensagens;
        this.cmbEquipe.DisplayMember = "nomeEquipe";

    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        conexão.desconectar();
    }
}
    
asked by anonymous 10.10.2018 / 04:21

1 answer

0

I discovered my error, the query should be in the OleDbDataAdapter and the OleDbCommand line does not exist. At the end the code looks like this:

private void carregacombo()
    {
        Conexao conexão = new Conexao();
        try
        {
            conexão.conectar();
            OleDbDataAdapter da = new OleDbDataAdapter("select idEquipe,nomeEquipe from Equipe", conexão.cn);
            DataTable dtMensagens = new DataTable();
            da.Fill(dtMensagens);
            this.cmbEquipe.DataSource = dtMensagens;
            this.cmbEquipe.ValueMember = "idEquipe";
            this.cmbEquipe.DisplayMember = "nomeEquipe";

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            conexão.desconectar();
        }
    }
    
10.10.2018 / 04:27