Inserting numeric data into the database

0

I am having trouble inserting into the database the field is formatted as DECIMAL(10,2) ), when I try to enter the direct monetary value through MySQL it works with the end point instead of the comma ex:

135.45 - The bank addresses the two decimal places.

Now when I try to insert through my program, it writes to the flock as follows:

13545.00

Follow my template.cs for this field

class Modelo
{
 private float nValor;

  public float Valor

        {
            get { return nValor; }
            set { nValor = value; }
        }
}

The action of the button to save the typed value is as follows:

private void btn_cadastrar_Click(object sender, EventArgs e)
        {
            Modelo mo = new Modelo();
            conexao con = new conexao();

            try
            {


                if (cbox_pagamento.SelectedItem.ToString() == "a vista")
                {
                    mo.Data_Compra = txt_dcompra.Text;
                    mo.Data_Alvo = txt_dalvo.Text;
                    mo.Fornecedor = txt_fornecedor.Text;
                    mo.Valor = float.Parse(txt_valor.Text);
                    mo.Tipo = cbox_tipo.Text;
                    mo.Pagamento = lbl_fiado.Text;
                    mo.Data_Pagamento = txt_dpagamento.Text;

                    con.cadastro(mo);

                    txt_fornecedor.Text = "";
                    txt_valor.Text = "";
                    MessageBox.Show("Dados gravados com sucesso!");
                }
                else
                {
                    mo.Data_Compra = txt_dcompra.Text;
                    mo.Data_Alvo = txt_dalvo.Text;
                    mo.Fornecedor = txt_fornecedor.Text;
                    mo.Valor = float.Parse(txt_valor.Text);
                    mo.Tipo = cbox_tipo.Text;
                    mo.Pagamento = lbl_fiado.Text;
                    mo.Data_Pagamento = txt_dpagamento.Text;

                    con.cadastro_aprazo(mo);

                    txt_fornecedor.Text = "";
                    txt_valor.Text = "";
                    MessageBox.Show("Dados gravados com sucesso!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Falha ao salvar no banco de dados :" + ex);
            }


        }

Does anyone have any idea how I can insert the value correctly into the database?

    
asked by anonymous 11.09.2017 / 16:58

3 answers

0

It may be because of commas in your field that are not identified in the conversion. Try changing the line

mo.Valor = float.Parse(txt_valor.Text);

by

mo.Valor = float.Parse(txt_valor.Text.Replace(',','.'));

    
11.09.2017 / 17:20
1

The first thing you need to change is the correct type in the code. You started doing well in the database, keep this in the code:

public class Modelo {
    public float Valor { get; set; }
}

Then do this:

private void btn_cadastrar_Click(object sender, EventArgs e) {
    var mo = new Modelo();
    var con = new conexao(); //espero que esteja bem feito para não dar vazamento
    try {
        if (Decimal.TryParse(txt_valor.Text.Replace(',', '.'), out var valor) {
            mo.Data_Compra = txt_dcompra.Text;
            mo.Data_Alvo = txt_dalvo.Text;
            mo.Fornecedor = txt_fornecedor.Text;
            mo.Valor = valor;
            mo.Tipo = cbox_tipo.Text;
            mo.Pagamento = lbl_fiado.Text;
            mo.Data_Pagamento = txt_dpagamento.Text;
            con.cadastro(mo);
            txt_fornecedor.Text = "";
            txt_valor.Text = "";
            //aqui precisa fazer a gravação
            MessageBox.Show("Dados gravados com sucesso!");
    } catch (MySqlException ex) { //capture uma exceção mais específica
        MessageBox.Show("Falha ao salvar no banco de dados :" + ex);
    }
}

Note that I captured the most specific exception that is the correct exception. But I do not know if the connection will not hang there. I think it will, but only with this excerpt I can not speak. You have to fix this.

I deleted the if that did the same thing. But I put another to get a value typo . And I made with decimal which is the correct type for monetary values .

I did the manual inversion of semicolon, but this is not correct, it is better to use culture. If you are interested I can show you that it is a bit more advanced, but it is less gambiarra.

I placed GitHub for future reference .

    
11.09.2017 / 17:24
0

After much searching I figured out how to convert, 1st in your bank will have to put the value field as Decimal (10,2) 2º at the time you are going to give the Insert into you have to replace the comma by the point. It looks more or less like this

string value = textboxvalor.text; value = value.replace (',', '.');

The insert will look like this: INSERT INTO TABLE (column_name) values ('"+ value +"');

Remembering that I use Visual Studio 2017 might change a bit if I use other software

    
27.09.2017 / 16:16