如何从FormatException确定详细信息

时间:2016-06-15 14:48:37

标签: c# mysql asp.net exception

我有一个连接到MySQL数据库的C#asp.net Web应用程序。与一个用户间歇性地,我在尝试列出数据库中的结果时收到System.FormatException。

当我们在下面的函数中调用products.ToList()时发生异常:

    private void ReloadCache()
    {
            var products = _context.Products.
                Include(p => p.Category).
                Include(p => p.Protocols).
                Include(p => p.LocalizedDetails.Select(pl => pl.Language)).
                Include(p => p.Costs.Select(pc => pc.Region.Currency));

            _productCache = products.ToList(); // exception thrown here
    }

在堆栈跟踪中,我可以看到在尝试从数据库中读取值并将其转换为小数时实际抛出了异常:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.TypeInitializationException: The type initializer for 'Cylon.QuoteEngine.Application.Web.BasePage' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Convert.ToDecimal(String value, IFormatProvider provider)
   at System.Convert.ToDecimal(Object value)
   at MySql.Data.MySqlClient.MySqlDataReader.GetDecimal(Int32 i)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetTypedValueDefault(DbDataReader reader, Int32 ordinal)
   at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at lambda_method(Closure , Shaper )
   at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
   at System.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
   at System.Data.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Cylon.QuoteEngine.Persistence.ProductRepository.ReloadCache()

我可以使用try catch块捕获FormatException,但是我没有更多关于什么字符串无法转换,或者它正在读取什么表或列的信息。它只是表明问题已经发生,我不是更明智的。

此应用程序已经运行了几年,并且直到最近只有一个用户才开始间歇性地发生此问题。 我检查了数据库中的数据,并确保类型符合预期。

有没有办法获取有关字符串,列,表格或其他任何可用于确定发生情况的信息?

0 个答案:

没有答案