I have a Web project which has data of different tools, Im able to insert and delete tools into a RadGrid (which is connected to a database), but Im having issues with the edit command
One of the values of the tools is "Cost" which is decimal, when Im trying to edit, half of the time it gets me this error:
"Input String was not in correct format error"
This is my Edit Method when the user clicks on the edit form modal to submit the edit
protected void btnMdlEditar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal;
var costo = editCosto.Text;
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
try
{
var id = CDatos.DHerramientas.UpdateHerramientas(int.Parse(txtId.Text), editCodigo.Text, editNombreCorto.Text, editDescripcion.Text, costoDecimal, editConsumible.Text);
rgHerramientas.DataBind();
rgHerramientas.Rebind();
upHerramientas.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex) { this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramientaEdit");
}
In the line
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
Is where I get the error, with a FormatException poping up, mentioning an issue while parsing a string to DataTime, for some reason. I have no idea why Im getting this error when Im using the exact code to parse decimal from string in my Insert Method
protected void btnAutorizar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal;
var costo = txtCosto.Text;
costoDecimal = decimal.Parse(costo.ToString(),style,provider);
try
{
var id = CDatos.DHerramientas.InsertHerramientas(txtCodigo.Text, txtNombreCorto.Text, txtDescripcion.Text, costoDecimal, txtConsumible.Text);
this.upDatos.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex){this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramienta");
}
When I edit the data by only deleting some a part of the number or when the number is not changed it throws the error, but if I delete the number completely and enter another decimal number, it allows me to edit it
7.85 to 7.89 = Error
7.85 to 6.12 = No error
Im already have
using System.Globalization;
And my current culture is "es-ES"
Edit: I tried to delete the "provider" from
costoDecimal = decimal.Parse(costo.ToString(), style, provider); which allows me to edit succesfully if I only change part of the number, but if I type a new number using "." it throws me the error again
I think the issue is using "." instead of "," which is the method used in Spain for decimals, is there a way to use both "." and ","?
Got it, the issue origin was using "," instead of "."
Because of the nature of my project I have to use culture as "es-ES" for the entire project.
If "." was used instead of "," it threw the error, so to be able for the users to use "." and the system to use "," I had to change the code
protected void btnMdlEditar_OnClick(object sender, EventArgs e)
{
NumberStyles style;
CultureInfo provider;
style = NumberStyles.AllowDecimalPoint;
provider = new CultureInfo("en-US");
decimal costoDecimal = 0;
var costo = editCosto.Text;
if (editCosto.Text.Contains("."))
{
costoDecimal = decimal.Parse(costo.ToString(), style, provider);
}
else if (editCosto.Text.Contains(","))
{
costoDecimal = decimal.Parse(costo.ToString(), style);
}
try
{
var id = CDatos.DHerramientas.UpdateHerramientas(int.Parse(txtId.Text), editCodigo.Text, editNombreCorto.Text, editDescripcion.Text, costoDecimal, editConsumible.Text);
rgHerramientas.DataBind();
rgHerramientas.Rebind();
upHerramientas.Update();
this.ShowMessage(Resources.Language.mess_insert, "success");
}
catch (Exception ex) { this.ShowMessage(ex.Message, "danger"); }
this.CloseModal("mdlHerramientaEdit");
}
Changing the culture depending if "," or "." was used