How to change the function of my save button, to change

Asked

Viewed 278 times

0

I have some data registered in the database, and I wanted to know how to change the function of inserting in the database to change the data, using a Button, I can pull the dice and throw them at us TextBoxs through the DataGrid, but wanted to change and save the amendments

Here I use my button btnSalvar to insert into the bank

 private void btnSalvar_Click(object sender, EventArgs e)
    {
        // campos vazios
        if (txtNome.Text.Equals(""))
        {
            System.Windows.Forms.MessageBox.Show("O nome do produto está vazio, por favor digite algo");
        }
        else if (txtPreco.Text.Equals(""))
        {
            if (txtPreco.Text.Equals(""))
            {
                System.Windows.Forms.MessageBox.Show("O Preço do produto está vazio, por favor digite algo");
            }
            else
            {

            }
            //fim dps campos vazios
        }            
        //eniando para o banco
        else
        {
            try
            {
                conectar.Open();
                //Convertendo

                Converter = Convert.ToDecimal(txtPreco.Text);

                //MessageBox.Show("Conectado");

                MySqlCommand Inserir = new MySqlCommand();
                Inserir.Connection = conectar;
                Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco, `status`) VALUES (@peca, @nome, @quantidade, @dataentrada)";

                Inserir.Parameters.AddWithValue("@peca", txtNome.Text);
                Inserir.Parameters.AddWithValue("@nome", txtDescricao.Text);
                Inserir.Parameters.AddWithValue("@quantidade", txtPreco.Text);
                Inserir.Parameters.AddWithValue("@dataentrada", ckbAtiv.Checked);

                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);
            }
        }
    }

And here I can play the values of DataGrid for the TextBoxs

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        txtNome.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        txtDescricao.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        txtPreco.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

    }
  • @Cypherpotato edited the question to try to make it clearer

1 answer

2

You can create a condition by specifying whether the object has already been saved, and if true, indicates that the button will make a change.

bool informacoesSalvas = false;

private void btnSalvar_Click(object sender, EventArgs e)
{
    if(informacoesSalvas)
    {
         // código para salvar o objeto
         ...
         informacoesSalvas = true;
    } else {
         // código para inserir no objeto
    }
}

To avoid code repetition, apply conditionals outside the execution of the database command block, thus:

if(string.IsNullOrWhiteSpace(txtNome.Text)) {
    System.Windows.Forms.MessageBox.Show("O nome do produto está vazio, por favor digite algo");
    return; // sai do método
}
if(string.IsNullOrWhiteSpace(txtPreco.Text)) {
    System.Windows.Forms.MessageBox.Show("O Preço do produto está vazio, por favor digite algo");
    return;
}

if(informacoesSalvas)
{
    // código para salvar o objeto
   ...
    informacoesSalvas = true;
} else {
    // código para inserir no objeto
}

This is what your code would look like.

  • how would the comment code // Código para Salvar o objeto like, I have to insert, it would be the same thing ?

  • @Pietronunciaroni practically its entire code within the Try...Catch. You need to do two implementations for the database: what saves the object, and what updates the object. There in the code you can see these implementations.

  • 1

    he presented another error, edited the question, along with the new code you advised

  • 2

    @Pietronunciaroni, this does not follow the rules of Stack Overflow. You should consider whether my answer answered the question to your question. If a new error happened, other than the one that was asked, a new question must be created, indicating this new error. If you want, you can put a link to this question, showing what has been done before. I will reverse your edits and hope you create a new question, so you receive a new resolution.

  • Thank you, sorry I got confused a little bit, I created a new question and already helped me there, sorry for the inconvenience

  • a, a question before, what really goes within each comment in the code ?

  • @Pietronunciaroni would be the nominee to put the content that is inside your block try...catch. The first would be one, with SQL implementation for save the file, and the bottom one would be the right one for update the file.

  • 1

    I’m sorry, I don’t quite understand that question yet, I don’t know how he could be, I can’t find any example of that either

  • In your own code you have the example of this. It is inside the block try...catch.

  • 1

    Well I ended up doing it in a different way, ended up working, but it still doesn’t change in the bank, he gives the message successfully altered! but still no change in the bank follows the Links, if you can take a look and help me thank you

Show 5 more comments

Browser other questions tagged

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