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
});
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
– Gabriel
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?– Leandro Angelo
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.
– João
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 ...
– novic
How you enter the information on Windows and how you type information on MAC?
– novic
in the same way, using the "." by having "," I get error on both of them
– João