2
I have two tables in postgres (PRODUCT and CATEGORY)
In the PRODUCT table I have the FIELDS -> ID, product name, description, value, Id_categoria.
In the CATEGORY table I have the FIELDS -> Id_category and Category.
In my Visual Studio project, I created a form to record new categories. Informing only an ex NAME. (Perfume, Accessories etc).
And I also have a form for registration of PRODUCTS where it is necessary to inform the product name, description, value and select a category code that fits (choose on combobox)
My problem is now: I cannot save the PRODUCT form because COMBOBOX is related to the CATEGORY NAME, because the person needs to know what will select and not see the codes. And in POSTGRES the field is INTEGER (Id_category), so it does not save the NAME.
PRODUCT form LOAD event. (old)
private void CadastroProduto_Load(object sender, EventArgs e)
{
banco conexao = new banco(); // Classe de conexão.
// Como carregar dados que estão no PostgreSQL no ComboBox
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conexao.conecta(); // Abre conexão
cmd.CommandText = "Select * from categoria"; // Seleciona a tabela
cmd.ExecuteNonQuery(); // Executa a busca
try
{
NpgsqlDataReader ler = cmd.ExecuteReader(); ; // Leitura de dados
while (ler.Read()) // Enquanto tiver campos para ler
{
cmbCategoriaProduto.Items.Add(ler["nome"]);
}
ler.Close();
ler.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
conexao.desconecta();
}
Updated FORM LOAD event (Updated: 05/10):
private void CadastroProduto_Load(object sender, EventArgs e)
{
banco conexao = new banco(); // Classe de conexão.
// Como carregar dados que estão no PostgreSQL no ComboBox
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conexao.conecta(); // Abre conexão
cmd.CommandText = "Select cod_categoria, nome from categoria"; // Seleciona a tabela
cmd.ExecuteNonQuery(); // Executa a busca
try
{
NpgsqlDataReader ler = cmd.ExecuteReader(); ; // Leitura de dados
DataTable dt = new DataTable();
dt.Columns.Add("cod_categoria", typeof(string));
dt.Columns.Add("nome", typeof(string));
dt.Load(ler);
cmbCategoriaProduto.DisplayMember = "nome";
cmbCategoriaProduto.ValueMember = "cod_categoria";
cmbCategoriaProduto.DataSource = dt;
// while (ler.Read()) // Enquanto tiver campos para ler
//{
// cmbCategoriaProduto.Items.Add(ler["nome"]);
// }
ler.Close();
ler.Dispose();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
conexao.desconecta();
}
Combobox Selectedindex Event
private void cmbCategoriaProduto_SelectedIndexChanged(object sender, EventArgs e)
{
string id = cmbCategoriaProduto.SelectedValue.ToString();
}
Still Error While Saving: Error:
Attributes and methods
// Atribulos
private string Nome;
private string Descricao;
private double Valor;
private string cCategoria;
//metodo construtor
public produto(string pn, string pd, double pv, string pccat)
{
Nome = pn;
Descricao = pd;
cCategoria = pccat;
Valor = pv;
}
Product Class - Include Method();
public void IncluirProduto()
{
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = conexao.conecta(); // Instacia o metodo conecta() que está na classe BANCO
cmd.CommandText = "Insert into produto (nome, descricao, cod_categoria, valor) values(@nome,@descricao,@cod_categoria,@valor)";
cmd.Parameters.Add(new NpgsqlParameter("@nome", Nome)); // atributo e campo declarado banco de dados.
cmd.Parameters.Add(new NpgsqlParameter("@descricao", Descricao));
cmd.Parameters.Add(new NpgsqlParameter("@cod_categoria", cCategoria));
cmd.Parameters.Add(new NpgsqlParameter("@valor", Valor));
cmd.ExecuteNonQuery();
conexao.desconecta(); // instancia o metodo desconecta() que está na classe BANCO
}
Sign Up Button
private void btnCadastrarProduto_Click(object sender, EventArgs e)
{
try
{
produto pdt = new produto(txtNomeProduto.Text, txtDescricaoProduto.Text, Convert.ToDouble(txtValorProduto.Text),cmbCategoriaProduto.Text);
pdt.IncluirProduto();
MessageBox.Show("Produto Incluso com sucesso!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Give a read on your question, this confused medium. I could not understand right what the problem and what would be the expected result. The code you posted doesn’t seem to have anything to do with your doubt.
– Christian Beregula
See if it’s clearer @Christianberegula
– WSS
I’ll give you a hint, the code you had in the question has some robustness issues but it has nothing to do with your problem. And I don’t think the problem is so clear yet. Is it an SQL only problem? Case of picking foreign key?
– Maniero
@Sorry it took me so long to get back to you. So I applied exactly how the msdn website does, even so qnd will record the error.
– WSS
What error appears? this Selectedindexchanged event is just that? if the error is there, updating the id as a local variable. You can post the SAVE event?
– Christian Beregula
@Christianberegula put the photo of the error that, basically is that the field has to record int, but I do not know where else
– WSS
The code of the button REGISTER, put it. the error is certainly there.
– Christian Beregula
@Christianberegula I updated
– WSS
the error is here ->
cmbCategoriaProduto.Text
where you create the new product– Christian Beregula
I don’t know what else to do. I have to convert to int. I’ve tried but Formatexception error
– WSS