Sequence contains no elements, ERROR

rob m 256 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:

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,374 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.1K 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 256 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;