How to load values in the combobox by the form load event C#

Asked

Viewed 318 times

2

I’m trying through this code:

private void frmAdicionarProdutos_Load(object sender, EventArgs e)
        {
             string serverName = "localhost";
             string port = "5432";
             string userName = "postgres";
             string password = "adm";
             string databaseName = "GE";
             NpgsqlConnection conn = null;
             string ConnString = null;

             ConnString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                           serverName, port, userName, password, databaseName);

            using (conn = new NpgsqlConnection(ConnString))
            {
                conn.Open();

                string cmdCarregar = String.Format("SELECT PRODUTOS.ID_PRODUTO, PRODUTOS.NOME, PRODUTOS.PRECO FROM PRODUTOS;");
                using (NpgsqlCommand cmd = new NpgsqlCommand(cmdCarregar, conn))
                {
                    NpgsqlDataReader dr = cmd.ExecuteReader();
                    DataTable dt = new DataTable();
                    dt.Load(dr);

                    cbProdutos.DisplayMember = "nome";
                    cbProdutos.ValueMember = "id_produto";
                    cbProdutos.DataSource = dt;
                    conn.Close();
                }     
            }
        }

At first I just want to test this way anyway, then implement by calling the bank class.

This code does not show anything in Combobox, I think I forgot something.. I would like to show: Product name + value of it (concatenated).

  • Probably the query should be like this: string cmdCarregar = String.Format("SELECT ID_PRODUTO, NOME, PRECO FROM PRODUTOS;");

  • In the same, I tested the query in postgres and returned me what I needed, but the combobox shows nothing yet..

  • 2 points, 1 do not need to use String.Format . 2 as you yourself said you need concatenate the PRODUCTS.NAME, PRODUCTS.PRICE .

2 answers

0

I can only see one cause for trouble: you nay associated the method frmAdicionarProdutos_Load to the event Form.Load

this.Load += new System.EventHandler(this.frmAdicionarProdutos_Load);

I tested your code here and the combo loaded right.

  • I made this change too and didn’t load it anyway.. load I think you’re right.. because I went to the events and the only load you have was the one that implemented the code

  • Weird. I tested your code and it worked. I just made a change to the query to take advantage of a table I already had. My query looked like this: string cmdCarregar = String.Format("SELECT \"ID\", \"Descricao\" FROM \"SchemaTeste\".\"TabTeste\";");

  • Another detail: I’m using the Npgsql versão 3.2.5, You came to check if the code of your routine is running?

  • How can I verify this? It would be by Breakpoint but I don’t know how to use it in this case

  • I used them, and it turned out Items.Count = 0

  • Yeah, put a Breakpoint pressing F9 over a line within Function. If the execution is not interrupted, then your routine is not running.

  • So it’s running.. Just don’t give the return

Show 3 more comments

0

I believe it will work when adding Databind()

cbProdutos.DataBind();

Make sure it works.

private void frmAdicionarProdutos_Load(object sender, EventArgs e)
    {
         string serverName = "localhost";
         string port = "5432";
         string userName = "postgres";
         string password = "adm";
         string databaseName = "GE";
         NpgsqlConnection conn = null;
         string ConnString = null;

         ConnString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                       serverName, port, userName, password, databaseName);

        using (conn = new NpgsqlConnection(ConnString))
        {
            conn.Open();

            string cmdCarregar = String.Format("SELECT PRODUTOS.ID_PRODUTO, PRODUTOS.NOME, PRODUTOS.PRECO FROM PRODUTOS;");
            using (NpgsqlCommand cmd = new NpgsqlCommand(cmdCarregar, conn))
            {
                NpgsqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);

                cbProdutos.DisplayMember = "nome";
                cbProdutos.ValueMember = "id_produto";
                cbProdutos.DataSource = dt;
                cbProdutos.DataBind();
                conn.Close();
            }     
        }
    }
  • There’s no such thing for me .DataBind() only .DataBindinds but tested and did not load it anyway.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.