2

I have a C# asp.net web application which connects to a MySQL database. Intermittently with one user, I am getting a System.FormatException when trying to list the results from the database.

The exception occurrs when we call products.ToList() in the function below:

    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
    }

In the stack trace, I can see the exception is actually thrown when trying to read a value from the database, and converting it to a decimal:

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()

I can catch the FormatException with a try catch block, but I do not have any more information on what string is failing to convert, or what table or column it is reading from. It merely states the issue occurred and I am none the wiser.

This application has been running for a couple years already, and only recently with one user has this issue started to intermittently occur. I have inspected the data in my DB, and ensured that the types match what is expected.

Is there a way to get information on the string, column, table or anything else that I can use to determine what is happening?

15
  • 1
    Have you run the SQL manually or profiled it to see what is being generated? Commented Jun 15, 2016 at 14:55
  • @WilliamXifaras Sorry I'm not sure I understand... Can you clarify what you mean? Commented Jun 15, 2016 at 15:00
  • @ChrisJ The LINQ to SQL is converted to SQL and run on the database. If you profile the query, you can find out what the exact SQL query being run against the database is and can then reproduce the issue. Commented Jun 15, 2016 at 15:03
  • Is there anything in the FormatException's InnerException property? Commented Jun 15, 2016 at 15:08
  • 1
    @ChrisJ, looks like there's some localization going on in your app? Could there be a monetary value stored in the DB that uses a currency symbol or currency separator that is incompatible with the language/region under which the process runs for the particular user who is experiencing the problems? This might cause ParseDecimal to fail. Commented Jun 15, 2016 at 15:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.