【问题标题】:C# Input string was not in a correct formatC# 输入字符串的格式不正确
【发布时间】:2015-05-28 19:46:51
【问题描述】:

运行以下代码时,有时会收到 FormatException 错误。异常似乎是随机抛出的。我的团队成员在运行代码时看到了错误,并会通知我。在几秒钟内,我将尝试使用同一个应用程序插入相同的数据,而不是让它异常。我在下面附上了格式异常文本的示例。异常意味着我正在尝试将小数转换为 int32,但在我分配的以下值中,没有一个是小数或 int。有没有人遇到过这样的格式异常?

foreach (var strPriceFile in lstPriceFiles)
{
    var SoldToRecs = DbContext.RFQ_SoldTo_PriceLists.Where(us => us.UserId == ERepGuid && us.RFQ_SoldTo_CustNbr == strSoldTo[0] && us.RFQ_SoldTo_CustSeq == strSoldTo[1] && us.IngresFileName == strPriceFile);

    if (SoldToRecs.Count() == 0)
    {
        var PriceFile = new RFQ_SoldTo_PriceList();
        PriceFile.UserId = ERepGuid;
        PriceFile.RFQ_SoldTo_CustNbr = strSoldTo[0];
        PriceFile.RFQ_SoldTo_CustSeq = strSoldTo[1];
        PriceFile.IngresFileName = strPriceFile;
        PriceFile.CreatedBy_UserName = strCreatorName;
        PriceFile.CreatedBy_DateTime = DateTime.Now;
        PriceFile.Active_YN = true;

        DbContext.RFQ_SoldTo_PriceLists.InsertOnSubmit(PriceFile);
    }
}

FormatException:输入字符串的格式不正确。] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +12630933 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +224 System.ComponentModel.Int32Converter.FromString(String value, NumberFormatInfo formatInfo) +46 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfoculture, Object value) +497

[例外:90.90908893868948 不是 Int32 的有效值。] System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext 上下文,CultureInfo 文化,对象值) +8019613 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object&convertedObject) +938181 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object&convertedObject) +227 System.Web.Script.Serialization.ObjectConverter.AssignToPropertyOrField(Object propertyValue, Object o, String memberName, JavaScriptSerializer serializer, Boolean throwOnError) +321 System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 字典,Type 类型,JavaScriptSerializer 序列化器,布尔 throwOnError,Object&convertedObject) +1790 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object& convertObject) +115 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer 序列化器, Boolean throwOnError, Object&convertedObject) +227 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(字符串输入)+126 Telerik.Web.UI.RadListBox.LoadPostData(字符串 postDataKey,NameValueCollection postCollection)+184 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +1018 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2653

【问题讨论】:

  • 您正在尝试反序列化为 int 属性。
  • 例外没有骗你。字符串格式错误。捕获异常,显示字符串,你会看到格式有什么问题。
  • 该错误看起来像您的代码正在计算双浮点(不是十进制)数字 90.1(大约)。很可能生成 JSON 的任何东西都不会将双精度值修整为 int。

标签: c# formatexception


【解决方案1】:

您的问题可能是由 Google Chrome 放大/缩小问题引起的,只要您使用的是 RadScheduler、RadListBox 和 RadTreeView,如 Hristo Valyavicharski 中所述下面的链接。在这种情况下,您可以通过在 Google Chrome 上放大或缩小来重现此问题,然后再次执行回发。

可以在此处找到此问题的解决方法:
http://www.telerik.com/forums/system-formatexception-78e82e51af27#KkKRUtkEq0ST4A09XY_eZQ

解释可以看这里:
http://www.telerik.com/forums/system-formatexception-78e82e51af27#Ww7bQEwfgUiShf9gFFmxBg

以下是 Hristo Valyavicharski 提供的修复:

对于 RadScheduler:

<script type="text/javascript">
Telerik.Web.UI.RadScheduler.prototype.saveClientState = function () {
    return '{"scrollTop":' + Math.round(this._scrollTop) + ',
    "scrollLeft":' + Math.round(this._scrollLeft) + ',
    "isDirty":' + this._isDirty + '}';
} </script>

对于 RadTreeView:

<script type="text/javascript">
Telerik.Web.UI.RadTreeView.prototype.saveClientState = function () {
    return "{\"expandedNodes\":" + this._expandedNodesJson +
    ",\"collapsedNodes\":" + this._collapsedNodesJson +
    ",\"logEntries\":" + this._logEntriesJson +
    ",\"selectedNodes\":" + this._selectedNodesJson +
    ",\"checkedNodes\":" + this._checkedNodesJson +
    ",\"scrollPosition\":" + Math.round(this._scrollPosition) + "}";
} </script>

对于 RadListBox:

<script type="text/javascript">
    Telerik.Web.UI.RadListBox.prototype.saveClientState = function() {
        return "{" +
                    "\"isEnabled\":" + this._enabled +
                    ",\"logEntries\":" + this._logEntriesJson +
                   ",\"selectedIndices\":" + this._selectedIndicesJson +
                   ",\"checkedIndices\":" + this._checkedIndicesJson +
                   ",\"scrollPosition\":" + Math.round(this._scrollPosition) +
               "}";
    } </script>


希望这会有所帮助。

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 感谢您的建议。已更新。
猜你喜欢
  • 2021-11-24
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多