question

robm-4957 avatar image
0 Votes"
robm-4957 asked YijingSun-MSFT commented

Sequence contains no elements, ERROR


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:


dotnet-csharpdotnet-aspnet-mvcdotnet-entity-framework
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @robm-4957 ,
According to your description, I suggest you could check if the tblSWATScores database is null. And then single() will return the only element of a sequence. So,you need to check if your sequence has elements that meet the conditions. Your problem is the value return null.

Best regards,
Yijing Sun

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Robmerritt-1803 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey How would I add a new row and insert this data the else below doesnr seem to work ...table schema below that

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



SELECT TOP (1000) [ID]
,[SurveyID]
,[VariableID]
,[Value]
,[VarName]
,[Description]
FROM [swat].[dbo].[tblSWATScores]



0 Votes 0 ·
robm-4957 avatar image
0 Votes"
robm-4957 answered YijingSun-MSFT commented

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;

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It finds a unique row by SurveyID and VarName, and then update the Value of found row. It expects that such row always exists.

If the legacy code does not work with current data, then maybe the data are not good.


0 Votes 0 ·

Hi @robm-4957 ,

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;

This line means it will get the only value which the SurveyID is same with tblswatwaprecipitation.SurveyID and the VarName is same with "precipTotal" in the tblSWATScores database.
I suggest you must check if you have the data which meet the conditions in the tblSWATScores.

Best regards,
Yijing Sun

0 Votes 0 ·

does this make sense the e object is empty

137252-image.png


0 Votes 0 ·
image.png (85.3 KiB)

Hi @robm-4957
,The e variable is only visible within the LINQ expression itself. So while the debugger is on the assignment statement ,you won't see it. You can set a breakpoint on e.SurveyID then you will see the e value when it is hit.

Best regards,
Yijing Sun

0 Votes 0 ·