Saving fields with a comma

0

What do I need to do to save a currency value with a comma and send it to the comma.

As I stated the price field in the bank:

preco number(12,2),

Code:

class clnPrato {

 private string _preco;

 public string preco {
  get {
   return _preco;
  }
  set {
   _preco = value;
  }

 }

 public void Gravar() {
  string strQuery;
  strQuery = "INSERT INTO Prato";
  strQuery += (" VALUES(");
  strQuery += ("'" + _preco + "',");
  strQuery += (")");
  clnBancoDados ObjClnBancoDados = new clnBancoDados();
  ObjClnBancoDados.ExecutaComando(strQuery);
 }

Save button:

private void btnsalvar_Click(object sender, EventArgs e) {
 clnPrato Prato = new clnPrato();

 Prato.preco = maskpreco.Text;

 if (ObjOperacao == clnFuncoesGerais.Operacao.Inclusao) {
  Prato.Gravar();
 }
}
    
asked by anonymous 15.10.2016 / 21:25

1 answer

4

There is a problem of type, do not use a text property to save a monetary value. Changing that should help.

This code does not make much sense, it's complicated to enter something that's just the price, but that's fine. I do not know the structure used, I'll post as I would (in the right way, avoiding memory leak):

using (var connection = new OracleConnection(connectionString))
using (var command = new OracleCommand("insert into prato (preco) values (:Preco)", connection)) {
    command.Parameters.AddWithValue("Preco", Preco);
    command.Connection.Open();
    command.ExecuteNonQuery();
}

You can even abstract the connection, but you need to ensure it is finalized and parameterized to avoid SQL injection and not suffer from this .

Property

It's also much simpler to declare a property like this:

public decimal Preco { get; set; }

MaskedTextBox

If you still want to use MaskedTextBox in the presentation, you have to convert the value. When reading the data from the database you will have to convert to string , probably with ToString() giving format you want .

To convert the read value in the user interface to the decimal value you must try to parse . I've already replied .

Understand the difference between given the given data. The presentation you can do the way you want, the data has no comma or no, it is a number with integer part and decimal part. The data exists without formatting.

Culture

If you want to control or convert the format may be over the .Net Culture System .

Type conversion functions, especially between string and numeric types, allow you to choose which culture you want to use.

You can also determine the application-specific culture (actually the thread ) for general use and for the user interface, and not depending on the operating system configuration. For example:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");

Actually doing right requires a deeper knowledge. Already, developing software is not as simple as it sounds. There is a lot of detail that needs to be noted and various possible combinations, some that work well, some that do not work.

    
15.10.2016 / 21:48