I cannot insert in sqlServer table float data when they are greater than 1000

Asked

Viewed 52 times

0

I am not able to insert double variables when they are greater than 1,000.00 because of the comma that Apare and so sql recognizes that I am trying to insert two data, I tried to use the replace(",",".") method, but it did not work.

     if (txtDesconto.Text == string.Empty)
                    {
                        preco = preco - 0;
                    }
                    else
                    {
                        preco -= desconto;
                    }

                    string precoStr = preco.ToString("N", CultureInfo.CreateSpecificCulture("en-US"));
                    precoStr.Replace(",", "");

                    Conexao conexao = new Conexao();

                    var escolha = MessageBox.Show("O cliente efetuou o pagamento de " + preco.ToString("c") + " ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (escolha == DialogResult.Yes)
                    {
                        conexao.conectar();
                        int insere = conexao.executar($"INSERT INTO Vendas(data, carro, placa, idCliente, pago, formaPagamento, valorCobrado) VALUES('{dataAmericanFormat}','{venda.carro}','{venda.placa}','{venda.idCliente}', 1, '{venda.formaPagamento}', {precoStr})  ");
                        conexao.desconectar();

                    }

2 answers

2

Hello!

If you are storing a monetary value, it would make more sense to change the column type in the FLOAT for MONEY. This would eliminate the need to use the replace that you’re trying to make it work.

Try the following on:

ALTER TABLE Vendas
ALTER COLUMN valorCobrado MONEY;
  • Okay, it makes a lot of sense what Oce said and it would certainly work, but doing it would give some interference in the data that are already saved?

1

Try it this way:

preco.Valor.ToString(CultureInfo.CurrentCulture).Replace(",",".")

Remembering that you may have a problem if the currency format is different from PT-BR, and yet SQL Server itself can be configured for a different format.

  • I use this line price.Valor.Tostring(Cultureinfo.Currentculture). Replace(",",".") for it to save values so 200.00 and not so 200.00. But it has to show me how to configure the sql format

  • This is the correct format and SQL standard, better leave so and adpitar your application to format the value when you consult.

  • Yes that I already do but when I will insert a value that has comma it does not insert

Browser other questions tagged

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