locked
System.InvalidOperationException: 'The string contains no elements' RRS feed

  • Question

  • User662762443 posted

    I am getting this error message, when testing the log (message) for user, when he tries to insert a same information, inform that there is already a registration with the message that he is trying to insert, but the log does not work correctly.

    My error message: The sequence contains no elements.

    My code:

    if (imported)
                {
                    
                    log.Add (log.Max (m => m.Key) + 1, "A spreadsheet with the SEP # entered has already been imported.");
                    fileInvalidated = true;
                }

    Tin that in my other log code as I am reporting the line works. Now, this validation if it was imported (Inserted) it should display the message normally. My log is a text only. Before that, it checks the database if there is any data in the database, if it does send this message, otherwise the file is inserted.

    This is my other log that works normally. log.Add (int.TryParse (line ["index"] .ToString (), out int aux)? aux: log.Max (m => m.Key) +1, "The rod number entered in cell E the line "+ (line [" index "]) +" does not correspond to the vara and comarca informed.Please correct the system or the worksheet.

    Monday, July 23, 2018 5:32 PM

Answers

  • User-1003897979 posted

    My guess is that there are no log entries.  You can't take the Max of something that doesn't exist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 24, 2018 12:47 AM
  • User-330142929 posted

    HI RtaVix,

    From your description and the error message mentioned.

    System.InvalidOperationException: 'The string contains no elements'

    I think the log is an enumerable list. and its data list is empty before inserting. so we could not get its max value of the key.

    Feel free to let me know if you have any questions.

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 24, 2018 3:22 AM

All replies

  • User-1003897979 posted

    My guess is that there are no log entries.  You can't take the Max of something that doesn't exist.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 24, 2018 12:47 AM
  • User-330142929 posted

    HI RtaVix,

    From your description and the error message mentioned.

    System.InvalidOperationException: 'The string contains no elements'

    I think the log is an enumerable list. and its data list is empty before inserting. so we could not get its max value of the key.

    Feel free to let me know if you have any questions.

    Best Regards

    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 24, 2018 3:22 AM
  • User662762443 posted

    Exactly that friend Abraham Qian, as it is only to display the message to the user, after the change was as follows:

    if (imported)
                {
    
                    log.Add(-1, "A spreadsheet with the SEP number informed has already been imported. <br>");
                    fileInvalidated = true;
                }

    Since I'm using the dictionary, I realized that I had to use -1.

    Thanks for your valuable tip.

    Regards,
    Rtavix

    Friday, July 27, 2018 5:54 PM