Help with Decimal . Net Core 2.2 recording

Asked

Viewed 286 times

0

I have a system in .NET CORE 2.2 where I am facing a problem in recording decimal numbers in the database MySQL.

Using as an example the decimal 19.99:

  • By MAC writes correctly;
  • By Windows writes incorrectly the value: 1999.00;

I did a lot of research on some solutions, but none worked for me...

I believe it may be some kind of culture problem.

VIEW

<div class="form-group col-md-2">
  <label>Valor</label>
  <input asp-for="Valor" type="text" class="form-control" id="decimal" value="@try{ @ViewBag.Chamado.Valor} catch{}">
</div>

MODEL

public decimal Valor { get; set; }
public void Gravar()
{
    DAL objDAL = new DAL();
    string sql = string.Empty;
    if (Id != null)
    {
        sql = $"UPDATE chamados SET titulo='{Titulo}', descricao='{Descricao}', pa_id='{PA}', equipamento_id='{Equipamento}', valor='{Valor}', status_id='{Status}' WHERE id='{Id}'";
    }
    else
    {
        string Data_Abertura = DateTime.Now.ToString("yyyy/MM/dd");
        sql = "INSERT INTO chamados (titulo, descricao, pa_id, equipamento_id, data_abertura, valor, status_id)" + $" VALUES ('{Titulo}', '{Descricao}', '{PA}', '{Equipamento}', '{Data_Abertura}', '{Valor}', '{Status}')";
    }
    objDAL.ExecutarComandoSQL(sql);
}

CONTROLLER

[HttpGet]
public IActionResult NovoChamado(int? id )
{     
    if (id != null)
    {
        ViewBag.Chamado = new ChamadosModel().RetornarChamado(id);
    }
    CarregarDados();
        return View();
    }

    [HttpPost]
    [SuppressMessage("ReSharper", "Mvc.ViewNotResolved")]
    public IActionResult NovoChamado(ChamadosModel chamado)
    {
        if (ModelState.IsValid)
        {
            CarregarDados();
            chamado.Gravar();
            return RedirectToAction("Index");
        }    
        return View();
    }
    private void CarregarDados()
    {
        ViewBag.ListaEquipamentos = new ChamadosModel().RetornarListaEquipamentos(); 
        ViewBag.ListaPA = new PAModel().ListarTodosPA();  
        ViewBag.ListaStatus = new StatusModel().ListarTodosStatus(); 
    }
}

SETANDO CULTURA

 // Definindo a cultura padrão: pt-BR
 var supportedCultures = new[] { new CultureInfo("pt-BR") };
 app.UseRequestLocalization(new RequestLocalizationOptions
 {
       DefaultRequestCulture = new RequestCulture(culture: "pt-BR", uiCulture: "pt-BR"),
       SupportedCultures = supportedCultures,
       SupportedUICultures = supportedCultures
 });
  • 2

    Your code seems to be vulnerable to SQL Injection. Consider using Prepared statements in your queries. Example: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommandprepare.?view=netframework-4.8

  • 2

    The first thing is because you’re passing the field valor as string. What if you replace the interpolations with Sqlcommands and SQL Parameters that are typed?

  • Regarding SQL Injection I will give a studied, how and it will be used only internally on local server and not a priority at the moment. @Leandroangelo could explain to me how to do this? I’m still at the beginning of my studies and I didn’t understand what I should do in this case.

  • Priority is to do the code without SQL Injection, there is the right way to do it and you are on the wrong side and so you have other problems besides what Leandro mentioned ...

  • How you enter the information on Windows and how you type information on MAC?

  • in the same way, using the "." by having "," I get error on both of them

Show 1 more comment

1 answer

1


The problem seems to be that your input type is text, when actually it should be number, with the Steps attribute:

<input asp-for="Valor" type="number" step="any">
  • I had already tested so, in this way it works the recording, the problem and that when Seto so I can not load the values when I edit the request, in this case it is empty. That’s why I’m using the text field and using a jquery to avoid typing "," and formatting the numbers

  • I just tested again here changing the type to "number" and in windows continues the problem, on normal mac, only with the problem of when edit does not load

Browser other questions tagged

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