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;");
– Jéf Bueno
In the same, I tested the query in postgres and returned me what I needed, but the combobox shows nothing yet..
– WSS
2 points, 1 do not need to use String.Format . 2 as you yourself said you need concatenate the PRODUCTS.NAME, PRODUCTS.PRICE .
– Marco Souza