1
Man ComboBox
brings the ID Primário
table Produto
to be registered in a new table that is the Pacotes
.
So far so good, the problem is that if I select another record on ComboBox
it saves as if it were the first record of the table Produtos
.
Then I needed a help how to send the ID_Produto
second record.
I will put the code of the save button that makes the function of sending the data to the database (remembering that I am using Mysql), remembering that the cbxPeca
in the code is mine ComboBox
:
private void btnSalvar_Click(object sender, EventArgs e)
{
try
{
Selecionarproduto();
conectar.Open();
int DataEntrada = Convert.ToInt32(txtEntrada.Text);
int DataSaida = Convert.ToInt32(txtSaida.Text);
//MessageBox.Show("Conectado");
MySqlCommand Inserir = new MySqlCommand();
Inserir.Connection = conectar;
Inserir.CommandText = Inserir.CommandText = "INSERT INTO Pacote (ID_Produto, Nome, Quantidade, Data_entrada, Data_saida ) VALUES ("
+cbxPeca.SelectedValue.ToString() + ", '" + txtNome.Text + "', '"
+ txtQuantidade.Text + "', '" + DataEntrada + "', '"
+ DataSaida +"'); ";
Inserir.ExecuteNonQuery();
conectar.Close();
MessageBox.Show("Cadastro Realizado!", "Concluido",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
selecionarCategoria();
}
catch (SqlException)
{
MessageBox.Show("Falha na conexao!", "falha",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
Populating the combo box
private void Selecionarproduto(){
DataTable dtTabelas = new DataTable();
MySqlConnection conectar = new MySqlConnection("server=127.0.0.1;database=ProdPacote; Uid=root; pwd=1234;");
MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("select ID_Produto, nome from Produto", conectar);
DataSet DS = new DataSet();
mySqlDataAdapter.Fill(DS);
cbxPeca.DataSource = DS.Tables[0];
conectar.Close();
mySqlDataAdapter.Fill(dtTabelas);
//cbxPeca.DropDown;
//carrega as informacoes no combo
cbxPeca.DataSource = dtTabelas;
cbxPeca.DisplayMember = "nome";
cbxPeca.ValueMember = "ID_Produto";
}
What happens when you select another item from
ComboBox
? Does the grid line change? I didn’t quite understand your question...– João Martins
if I select another item and save, it marks as if it were the first
– Pietro Nunciaroni
Sorry @Pietro, but I still don’t understand... which item? In which control? It "marks" what and how?
– João Martins
When registering packages, it has a combo box that pulls the data shown in the given lower Grid, I want to save the "Id_product" of a given record in another table in the database, but even if I select another item in the Combo box it only saves the first record, you can see that in the upper Grid the two Records are as 1 in the same "Id_product"
– Pietro Nunciaroni
If you want @Joãomartins I can send you the project to take a look
– Pietro Nunciaroni
No need, I’ve got your problem! The property
cbxPeca.SelectedValue
is not giving the correct value. Edit your question and place the code where you are filling in theComboBox
, which is definitely where the problem lies.– João Martins
I edited @Joãomartins, I put the code I’m using to fill the
Combo Box
– Pietro Nunciaroni
The properties
ValueMember
,DisplayMember
andDataSource
Combobox are there to show your settings or are actually part of the code?– Augusto Vasques
they are part, they are the ones who carry the
Combo Box
– Pietro Nunciaroni
The problem is that it is re-assigning the
DataSource
when you click the record button, then it re-fills theComboBox
and moving into first position! Remove the lineSelecionarproduto();
right at the beginning of the eventbtnSalvar_Click
and the problem will be solved.– João Martins
Okay, I’ll try, Thank you @Joãomartins
– Pietro Nunciaroni
Thank you guys, it worked!
– Pietro Nunciaroni
@Joãomartins, I have a question, and if I wanted to take the
nome
ofProduto
instead ofID_Produto
– Pietro Nunciaroni
Then I think it would be enough to take the value of the text in
ComboBox
:cbxPeca.Text
– João Martins
Thanks @Joãomartins
– Pietro Nunciaroni