1
votes

I am getting this error, I know there are a lot of questions with this error but I believe this is a different case. Also, I have tried to clean some records from SQL server database so I think it is possible to be inconsistence database but the problem is I am not sure how to fix it? Any thoughts would be much appreciated. Thank you

 Server Error in '/' Application.

The 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 121:                                    </tr>
Line 122:                                </thead>
Line 123:  ->The error is here!<--       @foreach (var item in Model.UserAbsences.OrderByDescending(x => x.UserAbsenceActions.Max(y => y.CreatedAt)).Where(c => c.Status.Equals(1) || c.Status.Equals(2)))
Line 124:                                {
Line 125:                                    <tbody>

Source File: Views\Home\Index.cshtml    Line: 123

Stack Trace:


[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.Max(IEnumerable`1 source) +324
   System.Linq.Enumerable.Max(IEnumerable`1 source, Func`2 selector) +60
   ASP.<>c.<Execute>b__3_6(UserAbsenceVm x) in \Views\Home\Index.cshtml:123
   System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) +81
   System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) +21
   System.Linq.<GetEnumerator>d__1.MoveNext() +232
   System.Linq.WhereEnumerableIterator`1.MoveNext() +174
   ASP._Page_Views_Home_Index_cshtml.Execute() in Views\Home\Index.cshtml:123
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9849993
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163
2
You should check at the line number 123 is giving the output null. - Suraj Kumar
Yes I can see UserabsenceActions giving me output null because of deleted record I think in the database but How can I work around that >? - beetle_juice
Please add this in you view code Where(c => !string.IsNullOrEmpty(c.Status) && (c.Status.Equals(1) || c.Status.Equals(2))) - jishan siddique
I have tried that as well and I cannot convert to string - beetle_juice
This depends on your intended behavior, but .DefaultIfEmpty(defaultOrderValue).Max(...) should work in any case. - DevilSuichiro

2 Answers

2
votes

You could handle the null as shown below.

@if (Model != null)
{
  //Your original code here as in line number 123.
}
1
votes

Can you wrap the for loop to check what you want to loop on actually exists. I'm assuming Model != null here:

@{
    if(Model.UserAbsences.Any()) {
        foreach (var item in Model.UserAbsences.OrderByDescending(x => x.UserAbsenceActions.Max(y => y.CreatedAt)).Where(c => c.Status.Equals(1) || c.Status.Equals(2))) {
             //...do stuff...
        }
    }
}