Entering numerical data in the database

Asked

Viewed 866 times

0

I’m having trouble inserting into the database the field is formatted as DECIMAL(10,2)), when I try to insert the direct monetary value by Mysql works with the endpoint instead of the comma ex:

135.45 - The bank intends the two decimal places.

Now when I try to insert by my program, it records in the flock as follows:

13545.00

Follow my model.Cs for this field

class Modelo
{
 private float nValor;

  public float Valor

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

The action of the button to record the entered value is like this:

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 put the value in the bank correctly?

3 answers

1

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

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

Then do it:

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

I put in the Github for future reference.

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

I eliminated the if who did the same thing. But I put another one to pick up a value typing error. And I did with decimal which is the correct type for monetary values.

I did the manual semicolon inversion, but this is not the most correct, it is better to use culture. If you’re interested I can show you in this way that it’s a little more advanced, but it’s less common.

  • I did as you proposed but the problem occurred in the same way, When I inform the value in the system 135,15 or 135.15 he inserts in the bank 13515.00 do not know the reason. you know how I can try to fix this ?

  • So you need to give more details of how your code is, how the data is. Starting by telling you what the problem is. Maybe it’s s[the case of dividing by 100.

  • I will publish my entire code to have a look, https://pastebin.com/3CV5pK3Q - Purchase Form https://pastebin.com/tpPgT06t - Project Template https://pastebin.com/tABv1Xe - Project Connection

  • 1

    Then I try to take a look, but this code is all full of repetitions and other weird things. If I were you I would try to learn the basics before trying to do something sophisticated.

  • I try to learn by doing in practice, taking tips and applying, seeing tutorials and etc. However I am very sorry

  • But it seems to be on the wrong track, there’s a huge amount of problems in that code. Usually going aimlessly like this doesn’t usually work out, I don’t know anyone who’s learned to program like this. You have to do it in a structured way, maybe with accompaniment of those who understand the subject. What is more on the Internet is people who do not know teaching wrong.

  • You have someone who can tell me that you have a web page or even a youtube page ?

  • These codes there are of a control of accounts to pay everything is working perfectly the only problem I discovered was that of the value, but I understand you must have a lot of unnecessary and repeated thing more it is practical

  • There is a big difference between working and being right. I wouldn’t call it perfectly. You can tell by the code that there are several errors. There are problems of validation and data entry, there are calculations that will generate rounding errors, there is wrong treatment of exception, repetition and other things that I could not even understand why it is there, only what I remember. But if you who are starting think that everything is perfect and the wrong is I who have 35 years of experience, then I can not help much.

  • I’m not saying at any point that you’re wrong, I just came to the forum to get help for a problem and can’t stand that I’m demotivating, I’m going to keep studying and I’m going to review the teaching methods better. But I wanted to know why it’s not working, why I haven’t figured it out yet. Anyway you’ve helped me.

  • I’m motivating you to do the right thing, and if you choose the wrong way, you’ll be discouraged when you realize that everything is falling apart and it’s not the way you were thinking. If you give me time I’ll see more.

  • All right, buddy, I’ve unfortunately been doing everything I can to find out, and I just couldn’t solve it. but no hurry when you have a little time and can take a look I would really appreciate it.

  • 1

    Print the entered value before converting or saving to the bank.

  • I’m sorry for the ignorance but I don’t understand.

Show 9 more comments

0

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

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

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

Remembering that I use Visual Studio 2017 may change a little if using other software

-2


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

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

for

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

  • Also tried this way the problem is at the time that enters the data via system in the database, example: when I inform in the system the value 135,15 or 135.15 it inserts in the database and is thus 13515.00. I don’t know what else to do :(

  • After a lot of research I figured out how to convert, 1º in your bank will have to put the value field as Decimal(10,2) 2º at the time you give Insert into vc has to replace the comma with the dot. It looks something like this: string value = textboxvalue.text; value = value.replace(',','.'); The Insert looks like this: INSERT INTO TABLE(column name_column)values('"+value+"'); Remembering that I use Visual Studio 2017 may change a little if I use other software

Browser other questions tagged

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