Sequence contains no elements, ERROR

rob m 261 Reputation points
2021-09-27T01:30:22.903+00:00

Not sure how to interpret this
db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipTotal").Value = precipTotal;135297-waprecipitationcontroller.txt
it is an old Entity Framework application that I am trying to reconstruct and get running.

Server Error in '/' Application.
Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Sequence contains no elements

> Source Error:
>
>
> Line 69: //robm
> Line 70:
> Line 71: db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipTotal").Value = precipTotal;
> Line 72: if (precipTotal != null)
> Line 73: {
>
> Source File: D:\code\SwatCORS\merrittr-swat-0bfb4e658cdd\SWAT_legacy\SWAT-Source Code\SWAT\SWAT\Controllers\WAPrecipitationController.cs Line: 71
>
> Stack Trace:

Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.5K Reputation points
    2021-09-27T09:11:29.287+00:00

    Probably the data are incomplete or invalid, because this error means that Single cannot find any row.

    To skip this step, try something like this:

    var s = db.tblSWATScores.SingleOrDefault(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipTotal");
    if( s != null) s.Value = precipTotal;
    

    Check other Single calls too.

    But also clarify why it does not return the items.


  2. rob m 261 Reputation points
    2021-09-28T04:01:36.223+00:00

    OK I will try that
    I dont undestand what this line is doing , is it updating a table based on the predicates inside the ()
    db.tblSWATScores.Single(e => e.SurveyID == tblswatwaprecipitation.SurveyID && e.VarName == "precipTotal").Value = precipTotal;


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.