System.FormatException: Input string was not in a correct format. RRS feed

  • Question

  • Some context: This error message appeared to the person I send an excel file to. I upload a file to Visual Studio and someone downloads and then imports that excel file into their system and because of this the formatting of the file cannot change.
    This is a new task for me, but what you would normally do is change some data in cells that are a General format. You do need to change the dates and this is where I think the error message arose from.
    The date columns are formatted to Date. If I click on a date cell, it is X Date layout and if I were to update the date using the formula bar keeping the layout the same, then press enter, the layout of the date changes. Which seems strange. Anyhow I hope this provides some context to help you - help me!

    Any help would be really appreciated. Thanks.

    Link to error message screenshot:
    Wednesday, April 15, 2020 6:59 AM

All replies

  • You will need to use a debugger to find the exact line of code where the error is occurring. You will find that it contains some kind of conversion from one type of data to another. The FormatException occurs when the data that it is converting is not recognized as a valid value for the target type. For example, this can happen if the value you are reading is stored by Excel as a numeric value, which Excel presents as a date on screen due to the cell formatting. Or it can happen if the value is stored as day/month/year but the program that converts it expects month/dat/year, or viceversa. This can be dependent on the regional settings that are configured for the computers where you write and read data, unless you force a specific format in the code that performs the conversion.

    So, as you see, there are multiple factors and possibilities. It's not feasible to know what exactly is happening and how to fix it without doing some debugging. Use the Visual Studio debugger (or instrument your program to do some logging if you can't attach the debugger) to locate the line where the error occurs and find the values (and types) of the data that is used in that line, and from there it will be possible to go ahead and find a way to fix it.

    Wednesday, April 15, 2020 7:29 AM