How to send the value of a combo box to the database

Asked

Viewed 317 times

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:

mostrando como é a tela e os registros

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...

  • if I select another item and save, it marks as if it were the first

  • Sorry @Pietro, but I still don’t understand... which item? In which control? It "marks" what and how?

  • 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"

  • If you want @Joãomartins I can send you the project to take a look

  • 1

    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 the ComboBox, which is definitely where the problem lies.

  • I edited @Joãomartins, I put the code I’m using to fill the Combo Box

  • The properties ValueMember, DisplayMember and DataSource Combobox are there to show your settings or are actually part of the code?

  • they are part, they are the ones who carry the Combo Box

  • The problem is that it is re-assigning the DataSource when you click the record button, then it re-fills the ComboBox and moving into first position! Remove the line Selecionarproduto(); right at the beginning of the event btnSalvar_Click and the problem will be solved.

  • Okay, I’ll try, Thank you @Joãomartins

  • Thank you guys, it worked!

  • @Joãomartins, I have a question, and if I wanted to take the nome of Produto instead of ID_Produto

  • Then I think it would be enough to take the value of the text in ComboBox: cbxPeca.Text

  • Thanks @Joãomartins

Show 10 more comments
No answers

Browser other questions tagged

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