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.
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.– novic
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.
– Almeida
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.
– enzo
@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.
– Maniero
@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.
– Maniero