Error saving monetary values in Mysql database

Asked

Viewed 202 times

1

I have a table column from my bank called salario_base where it is of the type Decimal(7,2). I enter the value in a textbox, I convert the same and pass this and other variables to an insert class to complete this operation. The other data is being saved perfectly, but the salary is not. Follow an example of what I am trying to save in the bank:

Ex: 1500.65

I try to perform the insertion shown in the above example, however only the value is saved 1500.00, when I do not use "."(point) saves 150065.00

the variable is declared in the application as a decimal type variable. Edit. Follows the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Sistema_RH.Classes;
using Sistema_RH.FORMULARIOS;
using Sistema_RH.NEGOCIOS;

namespace Sistema_RH.DADOS
{
    public class DAOFolhadePagamento
    {
        public void CadastrarPayment(string rsocial, string cnpj, string nomefunc, decimal sbase, string depart, string obs)
        {
            using (MySqlConnection connectaInBD = DAOConexao.getConnection())
                try
                {   
                    string RazaoSocial = rsocial;
                    string CNPJ = cnpj;
                    string NomeFuncionario = nomefunc;
                    decimal SalarioBase = sbase;
                    string Departamento = depart;
                    string Observacoes = obs;

                    string ComandoSQL = "INSERT INTO folhadepagamento (razao_social, cnpj, nome_do_funcionario, salario_base, departamento, observacoes)" +
                    " VALUES (@razao_social, @cnpj, @nome_do_funcionario, @salario_base, @departamento, @observacoes)";


                    System.Windows.Forms.MessageBox.Show(ComandoSQL);

                    connectaInBD.Open();
                    MySqlCommand inserttDados = new MySqlCommand(ComandoSQL, connectaInBD);

                    inserttDados.Parameters.Add("@razao_social", MySqlDbType.VarChar, 100);
                    inserttDados.Parameters.Add("@cnpj", MySqlDbType.VarChar, 30);
                    inserttDados.Parameters.Add("@nome_do_funcionario", MySqlDbType.VarChar, 100);
                    inserttDados.Parameters.Add("@salario_base", MySqlDbType.Decimal, 12);
                    inserttDados.Parameters.Add("@departamento", MySqlDbType.VarChar, 50);
                    inserttDados.Parameters.Add("@observacoes", MySqlDbType.VarChar, 500);

                    inserttDados.Parameters["@razao_social"].Value = RazaoSocial;
                    inserttDados.Parameters["@cnpj"].Value = CNPJ;
                    inserttDados.Parameters["@nome_do_funcionario"].Value = NomeFuncionario;
                    inserttDados.Parameters["@salario_base"].Value = SalarioBase;
                    inserttDados.Parameters["@departamento"].Value = Departamento;
                    inserttDados.Parameters["@observacoes"].Value = Observacoes;

                    inserttDados.ExecuteNonQuery();

                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }

                finally
                {
                    connectaInBD.Close();
                } 
        }

    }
}

Edit2: I have made the modifications suggested to me in the comments. However the value is still not being stored in the bank the way you wanted it to be (as stated at the beginning). I decided to insolar the textbox referring to the entry of this value and chapei the direct value in the variable Salario_Base.

Following example:
Ex.: decimal SalarioBase = 1500.55M; that M It was Visual Studio itself that suggested,
thus the insertion occurred and the value was saved as expected in the bank.
I thought about using the method Concat(txtSalarioBase.Text, "M");and then use the Convert.ToDecimal();. But it doesn’t seem to work, soon I didn’t even try to implement, I am following the instructions of the link above. I really don’t know what to do...

  • 3

    show your code

  • 3

    Here is not a forum, if you read to use Replace(), I’m sorry, we tried to make the answers have quality, but the ones that taught to do this taught wrong (unless there’s a context that I’m not aware of). Actually there is already a conceptual error there. But without the code we can even talk a lot, even less help.

  • @Maniero added the code.

  • There’s nothing decimal, nothing salary on it. I saw several other errors in the code.

  • @Maniero is now right, previously had copied and pasted the wrong class.

  • 3

    And if you do it right and remove the serious security problem from your code, the error continues? https://answall.com/q/104614/101, https://answall.com/q/183975/101

  • 2

    DON’T TRY, I REPEAT, DON’T FILL IN THE FIELD Observações with the following text '); TRUNCATE TABLE folhadepagamento; --

  • I ended up changing as @Maniero suggested me, the change will follow in this question with the changes suffered!

Show 3 more comments

1 answer

0

The code below is not so elegant, but I think it helps:

string ammount = (textBox2.Text).Replace (",",".");

and I enter the value as string. When the value is passed to the sql statement:

string initialquery = "INSERT INTO EXPENSES (ID, E_DATE, AMMOUNT, PERSON, COMMENTS, T_ID) " +
                                "VALUES ('" + id + "', '" + date.ToString("yyyy:MM:dd") + "', '" + ammount + "', '" + person + "', '" + comments + "', '" + tid + "')";

Original: c# mysql decimal Numbers with . or ,

Browser other questions tagged

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