Problem inserting double value in Mysql

Asked

Viewed 1,247 times

1

query += txtServidor.Text + "',";<br>
query += double.Parse(txtVencimento.Text) + ",";<br>
query += int.Parse(txtBanco.Text) + ",'";<br>

INSERT INTO tbl_associados VALUES(10,'testando','001.318.555-55','categoria','ativo',
    'MG-13.131.313','2010-07-10','ssp/mg','1999-07-07',20,'2014-12-10','M','SI','Solteiro','',
    'pai teste','mae teste','(31)3333-3333','(33)9999-9999','(37)9999-8888','email teste',
    'Avenida teste',444,'','bairro','cidade','30421-888','MG','07 -Timóteo','setor','cargo',
    'dez/2010',10025,1044,'1234','4','013','S','S');

The value "10025" in the last line should be "100.25", the txtVencimento.Text received the correct value 100.25, however to thresh the code realized that by concatenating the value with the rest of the query, the point was simply ignored (alone) and is going as I showed up on query.

In the database the maturity is of the type double(9,2) not null.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

  • Removing "Parse" can solve, since the variable 'query' is string and you will concatenate all values as string. There is no logic to convert to double/int if it will be concatenated as string.

2 answers

2

First of all, you swear you’re wearing double to manipulate monetary values? See that question. This goes for both the C#code and the database. You will cause financial and possibly tax problems, with legal implications for the company using this software.

Something tells me other guys are wrong, like this one int to the bank. But I can’t say much without seeing the whole and it’s not the focus of the question. I’m just warning you that your system has other serious problems and I couldn’t pass it up.

Another problem is that the format may not be correct and the parse can fail. Will you let an error occur? Or worse, accept an inappropriate value?

Your problem probably has to do with the culture you’re using, but I can’t guarantee because the question lacks information. If this is it, it might be gone answered here. So a possibility to solve this would be:

Decimal.TryParse(txtVencimento.Text, NumberStyles.Any,
        System.Globalization.CultureInfo.InvariantCulture, out valorDecimal)

But it may be that the culture needed for your case would be CultureInfo.CreateSpecificCulture("pt-BR"). It has full example and explanation in documentation. If this style is not the most suitable you can choose others according to the documentation.

No more details because the question doesn’t help.

But note that this solution tries to solve all problems: changes the double for decimal, tries to do the parse, but gives the chance to care if it fails and uses crop to correctly manipulate decimal point.

Of course if you do not want to follow my recommendation to solve all problems, you can use a double.Parse() simple only with the culture. It will be a mistake but it is your option.

0

Try to parse that way:

double.Parse(txtVencimento.Text, System.Globalization.CultureInfo.InvariantCulture)

Browser other questions tagged

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