Decimal is not accepting decimals - ASP.NET MVC 5

Asked

Viewed 63 times

0

I am doing a project in ASP.NET, but it started to give a problem, the field only accepts to receive integers, every time I put a decimal number, for example 20.10, it simply does not let me insert the Product. In the class it is as the name of precoUnitario and decimal.

Follows the Class:

public class Produto
{
    private ConexaoDB db;

    [Display(Name = "Código")]
    [StringLength(5, ErrorMessage = "Máximo 5 caracteres")]
    [Key]
    public string idProduto { get; set; }

    [Display(Name = "Nome")]
    [Required(ErrorMessage = "O campo é obrigatório")]
    [StringLength(50, ErrorMessage = "Máximo 50 caracteres")]
    public string nome { get; set; }

    [Display(Name = "Preço Unitário")]
    [Required(ErrorMessage = "O campo é obrigatório")]
    public decimal precoUnitario { get; set; }

    [Display(Name = "Categoria")]
    [Required(ErrorMessage = "O campo é obrigatório")]
    public string idCategoria { get; set; }

    public void InsertProduto(Produto produto)
    {
        string strQuery = string.Format("insert into produto(idProduto, nome, precoUnitario, idCategoria)" +
            "values('{0}', '{1}', {2}, '{3}');", produto.idProduto, produto.nome, produto.precoUnitario, produto.idCategoria);

        using (db = new ConexaoDB())
        {
            db.ExecutaComando(strQuery);
        }
    }
}

The Project is also connected to the bank, follows the table Product:

CREATE TABLE produto(
idProduto varchar(5) primary key,
nome varchar(50) not null,
precoUnitario decimal(10, 2) not null,
idCategoria varchar(5) not null,
foreign key (idCategoria) references categoria(idCategoria)
 );

How can I solve this problem?

  • Is there an error? Which one? Where does it occur? What value is added to the query (in strQuery)? If there is no error, is some value inserted when using "20.1"? And with "20.1", what happens?

  • In the box you have to type in the Brazilian format by the look 20,10 with comma and the algorithm itself does the heavy work for you, including, these names are out of the pattern that the initial and main of a name are written in upper case (this is not Java is C#) so PrecoUnitario would be an example of how to write names in C#

  • @Leo By any chance at some point, don’t you perform some mathematical operation with this value using an integer number? Like, some sum or multiplication?

No answers

Browser other questions tagged

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