C# Input string was not in a correct format. Conversion issue on server not in local machine

asp.netcjcrop

I am croppping the image and save. On btnsave_click I am converting the hiddenfield value to decimal. There is no error on local machine but when published to server it gives below error.

Detailed exception on server:

Input string was not in a correct format.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options,
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
+10726387 System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt) +172
System.Convert.ToDecimal(String value) +68
IngredientMatcher.Pages.ImageCropPopup.btnsave_Click(Object sender,
EventArgs e) +104
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552602
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1724

Code ::

protected void btnsave_Click(object sender, EventArgs e)
{
    string ImageName = ViewState["ImageName"].ToString();
    int www = Convert.ToInt32(Math.Round(Convert.ToDecimal(W.Value)));
    int hhh = Convert.ToInt32(Math.Round(Convert.ToDecimal(H.Value)));
    int xxx = Convert.ToInt32(Math.Round(Convert.ToDecimal(X.Value)));
    int yyy = Convert.ToInt32(Math.Round(Convert.ToDecimal(Y.Value)));
    int w = (www == 0) ? 0 : www;
    int h = (hhh == 0) ? 0 : hhh;
    int x = (xxx == 0) ? 0 : xxx;
    int y = (yyy == 0) ? 0 : yyy;

    byte[] CropImage = Crop(Server.MapPath(" ") + "\\" + upPath + ImageName, w, h, x, y);
    using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
    {
        ms.Write(CropImage, 0, CropImage.Length);
        using (SD.Image CroppedImage = SD.Image.FromStream(ms, true))
        {
            string SaveTo = Server.MapPath("") + "\\" + CropPath + "crop" + ImageName;

            CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
            imgCropped.BorderWidth = 1;
            imgCropped.ImageUrl = CropPath + "crop" + ImageName;
        }
    }
    string CroppedImg = "crop" + ViewState["ImageName"].ToString();

    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "OpenPopUp", "javascript:SaveAndClose('" + CroppedImg + "');", true);
}

Thanks in advance.

Best Answer

Your problem lies in the culture used to convert the decimal. Some cultures use 0,01 and some use 0.01. therein lies the problem.

You could use the invariant culture (always 0.01 as input) for example:

int www = Convert.ToInt32(Math.Round(Convert.ToDecimal(W.Value, System.Globalization.CultureInfo.InvariantCulture)));
int hhh = Convert.ToInt32(Math.Round(Convert.ToDecimal(H.Value, System.Globalization.CultureInfo.InvariantCulture)));
int xxx = Convert.ToInt32(Math.Round(Convert.ToDecimal(X.Value, System.Globalization.CultureInfo.InvariantCulture)));
int yyy = Convert.ToInt32(Math.Round(Convert.ToDecimal(Y.Value, System.Globalization.CultureInfo.InvariantCulture)));

Or you could use your culture, just replace System.Globalization.CultureInfo.InvariantCulture with CultureInfo.CreateSpecificCulture("nl-NL") where the constructor string is replaced with your culture.

Related Topic