Problem inserting double value in MySQL

0
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 was given the correct value of 100.25, but to debugar the code realized that by concatenating the value with the remainder of < in> query , the point was simply ignored and is going as I showed up there in query .

In the database, the due date is double(9,2) not null .

    
asked by anonymous 30.03.2015 / 14:58

2 answers

2

First, do you swear that you are using double to manipulate monetary values? See this question . this is true for both the C # code and the database. You will cause financial and possibly tax problems, with legal implications for the company that uses this software.

Something tells me that other types are wrong, such as int for the bank . But I can not say much without seeing the whole and is not the focus of the question. I'm just warning you that your system has other serious problems and could not pass blank.

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

Your issue probably has to do with the culture you are using, but I can not guarantee why the information is missing from the question. If this is it, it may have already been answered here . So one 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") . You have a complete example and an explanation on documentation . If this style is not appropriate, you can choose others according to documentation .

I do not put more details because the question does not help.

But note that this solution tries to solve all the problems: it changes the double to Decimal , tries to parse, but gives the chance to take care if it fails and uses culture to correctly manipulate the decimal point. p>

Of course, if you do not want to follow my recommendation to resolve all issues, you can use a double.Parse " simple only with culture. It will be a mistake but it is your choice.

    
30.03.2015 / 15:18
0

Try to parse this way:

double.Parse(txtVencimento.Text, System.Globalization.CultureInfo.InvariantCulture)
    
30.03.2015 / 15:17