2

I am developing a Asp.net MVC web application and have been deploying to a web server for a few months now. Just recently I have experienced a new error that confused me. Upon publishing my application, and loading up the website, I get the following error message:

 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: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[InvalidOperationException: Sequence contains no elements]
 System.Linq.Enumerable.First(IEnumerable`1 source) +514
System.Linq.Queryable.First(IQueryable`1 source) +330
LabSys.MvcApplication.Application_AuthorizeRequest(Object sender, EventArgs   e) +1261
  System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +165

I am very confused, because the application runs perfectly on localhost, without any issue or error message. I also checked my database, and it is also connected and validated to the server. So there should be no error.

Thanks for any help in advance!

Tanzeel ur Rehman
  • 429
  • 2
  • 6
  • 18
Eli
  • 533
  • 1
  • 5
  • 24
  • 1
    where in your code is this exception thrown and can you show some code? – Oluwafemi Jun 19 '15 at 19:17
  • That is the part that im stuck with. The error is not thrown anywhere when viewing the website through localhost. Once I view it through the published server, it is thrown on every single page. – Eli Jun 19 '15 at 19:22
  • Is this error is coming at the landing page/home page of your application. If i am not wrong you are querying database there and you are not checking whether the returned collection having any element are not and you are using it. – Tanzeel ur Rehman Jun 19 '15 at 19:25

1 Answers1

1

Check in your code to make sure FirstOrDefault() or SingleOrDefault() is used instead of using any of these: First() or Single().

When you get theLINQ Error "Sequence contains no elements", this is usually because you are using the First() or Single() command rather than FirstOrDefault() and SingleOrDefault().

Sequence contains no elements?

Community
  • 1
  • 1
Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
  • Thanks so much! I completely looked over a line of code I had in my Global.asax.cs file that is called on every request, causing a null value. – Eli Jun 19 '15 at 19:35