Saving fields with comma

Asked

Viewed 749 times

0

What I need to do to save in a field currency-like values with the comma and send to the with the comma.

As I declared 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();
 }
}
  • Enzo se.vc set up the database correctly, and made a mistake in the design of the class that should have the decimal type, because it has this wrong purpose to record with a comma? for example queries in this field to work if they are engraved with comma will need conversion being more costly decreasing performance. In your code you have a problem you should use paramenters and not pure SQL.

  • I use in Asp.net mvc# <globalization Culture="en-BR" uiCulture="en-BR"> in Web.config to write to ms sql server of decimal type(99.99) with comma, example: Precovenda (decimal(10.2), not null). Bounce that this is to record if it is to edit and show on the screen ai have other actions with respect to validation plugins.

  • In fact I want to bring the prices saved to make calculation, that’s why I want to save with comma. I put the field as a string, just to do tests, I knew it was wrong. I used SQL because that’s how I learned, I was told it was wrong and I’ve been told to use paramenters, I haven’t used it because I don’t know "what is" yet and I don’t know how to use it either. I’m giving a study.

  • @Don’t test the wrong way. This doesn’t help you learn, nor do people respond properly. When someone says they’re doing it wrong, change the way they do it. If you don’t know how to use it, check the manual, look for a book that teaches you how to do it right and even specifically ask about it, but don’t do it wrong. I’ll give you a little bit of an answer.

  • @Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

4

There is a type problem, do not use a text property to store a monetary value. Changing this should help.

That code doesn’t make much sense, it’s complicated to insert something that’s just the price, but okay. I do not know the structure used, I will post as I would (the right way, avoiding memory leakage):

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();

I put in the Github for future reference.

You can even abstract the connection, but you need to ensure its completion and parameterization for avoid SQL injection and not suffer this.

Estate

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

public decimal Preco { get; set; }

MaskedTextBox

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

To convert the value read in the user interface to the decimal value you need to try to make a parse. Already I answered that before.

Understand the difference between given data presentation. The presentation you can do as you like, the data has nothing of a comma or not, it is a number with an integer part and decimal part. The data exists without format.

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.

It is also possible to determine the specific crop of the application (in fact 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 it right requires a deeper knowledge. I would say that developing software is not as simple as it seems. There’s a lot of detail that needs to be observed and a lot of possible combinations, some that work well, some that don’t work.

  • I knew I was wrong. When I switch to decimal, the value it records is 0, even if I put another one. I am entering other values too, I only left the price to simplify. I took the Maskedtextbox and put a Textbox, I still have to convert?

  • Yes, note that their name contains a Text, I mean, it’s not value, so you need to convert. Of course you can create abstractions to help you do it transparently, but this is much more advanced.

  • If I were to use this example of yours, would it be inside the save button? This Thread code that you gave as an example, should I also use it? And if I were to use your example, I should delete my Record method?

  • @Not so much that he ignored this method. I don’t know if I should use the culture directly. It will depend on the implementation, the context. Again, you need to understand the whole to know when to use each thing. I can’t look at an excerpt, which actually doesn’t even work and tell you whether or not to use something that will change the entire application. I gave you an option, only you know if it is good for your case. So you must understand the full functioning of culture. Programming is not following a single recipe.

  • Okay. Thank you very much. And thanks for your patience.

Browser other questions tagged

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