C# 输入字符串的格式不正确。服务器上的转换问题不在本地机器上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17379083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Input string was not in a correct format. Conversion issue on server not in local machine
提问by Janty
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.
我正在裁剪图像并保存。在 btnsave_click 上,我将隐藏字段值转换为十进制。本地机器上没有错误,但是当发布到服务器时,它会出现以下错误。
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.
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
Exception Details: System.FormatException: Input string was not in a correct format.
异常详细信息:System.FormatException:输入字符串的格式不正确。
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.
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。
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
[FormatException: 输入字符串的格式不正确。]
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.
提前致谢。
采纳答案by SynerCoder
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.
您的问题在于用于转换小数的文化。有些文化使用 0,01,有些文化使用 0.01。问题就在这里。
You could use the invariant culture (always 0.01 as input) for example:
您可以使用不变区域性(始终为 0.01 作为输入),例如:
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.InvariantCulturewith CultureInfo.CreateSpecificCulture("nl-NL")where the constructor string is replaced with your culture.
或者你可以使用你的文化,只需更换System.Globalization.CultureInfo.InvariantCulture与CultureInfo.CreateSpecificCulture("nl-NL")在构造函数字符串替换为你的文化。
回答by Steve
You should be sure that the value converted are effectively numbers and not empty strings before trying to execute any conversion operation. To obtain this resultthe best approach is through the Decimal.TryParse method that checks if you have really a valid decimal number for your culture and then execute the conversions and math operations on the value
在尝试执行任何转换操作之前,您应该确保转换的值实际上是数字而不是空字符串。要获得此结果,最好的方法是通过 Decimal.TryParse 方法检查您的文化是否真的有一个有效的十进制数,然后对值执行转换和数学运算
decimal dw;
decimal dh;
decimal dx;
decimal dy;
int www = 0;
int hhh = 0;
int xxx = 0;
int yyy = 0;
CultureInfo ci = CultureInfo.CreateSpecificCulture("en-GB"); // Here your specific culture
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dw))
www = Convert.ToInt32(Math.Round(dw));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dh))
hhh = Convert.ToInt32(Math.Round(dh));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dx))
xxx = Convert.ToInt32(Math.Round(dx));
if(decimal.TryParse(W.Value, NumberStyles.AllowDecimalPoint, ci, out dy))
yyy = Convert.ToInt32(Math.Round(dy));
回答by Jorge Diaz
In the web.config, in system.websection add this, change culture to local region:
在web.config, 在system.web部分添加此内容,将文化更改为本地区域:
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="es-PA"
uiCulture="de-DE"
/>

