title: ASP.NET MVC Ajax - InvalidOperationException, sequence contains no elements
body: <p>Whenever I make an Ajax call to a certain method, it gives an InvalidOperationException with the message "sequence contains no elements", which is obvious as I want to verify that it gives an error when entering a non-existing article. </p>
  
  <p><strong>The Ajax which calls to the method</strong></p>
  
  <pre><code>$.ajax({
      url: '@Url.Action("GetArticleName", "Date")',
      data: JSON.stringify({ barcode: $("#ArticleBarcode").val() }),
  
      type: 'POST',
      contentType: 'application/json; charset=utf-8',
  
      dataType: 'json',
      success: function (result) {
          var divInsert = document.getElementById("ArticleName");
          divInsert.innerHTML = result;
          validArticle = true;
      },
  
      error: function (xhr, s, error) {
          if (xhr.status == 404) {
              var divInsert = document.getElementById("ArticleName");
              divInsert.innerHTML = "Varen blev ikke fundet";
          }
          else {
              alert("error: " + s);
          }
          validArticle = false;
      }
  });
  </code></pre>
  
  <p><strong>The method which returns the error</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult GetArticleName(long barcode)
  {
      ArticleDbContext artdb = new ArticleDbContext();
      String article = (String)(from art in artdb.Articles where art.Barcode == barcode select art.Name).SingleOrDefault();
      if (article == null)
      {
          return HttpNotFound();
      }
      return Json(article);
  }
  </code></pre>
  
  <p><strong>Stacktrace</strong></p>
  
  <pre><code>[InvalidOperationException: Sekvensen indeholder ingen elementer] 
  System.Linq.Enumerable.First(IEnumerable`1 source) +269
  System.Data.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__0(IEnumerable`1 sequence) +40
  System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable`1 query, Expression queryRoot) +59
  System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +133
  System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +123
  System.Linq.Queryable.First(IQueryable`1 source) +251
  DatoCheckerMvc.Controllers.DateController.GetArticleName(Int64 barcode) in c:\Users\frederik\Documents\Visual Studio 2012\Projects\DatoCheckerMvc\DatoCheckerMvc\Controllers\DateController.cs:66   lambda_method(Closure , ControllerBase , Object[] ) +165
  System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
  System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass42.&lt;BeginInvokeSynchronousActionMethod&gt;b__41() +28
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +10
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass39.&lt;BeginInvokeActionMethodWithFilters&gt;b__33() +57
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49() +223
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass37.&lt;BeginInvokeActionMethodWithFilters&gt;b__36(IAsyncResult asyncResult) +10
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2a.&lt;BeginInvokeAction&gt;b__20() +24
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;b__22(IAsyncResult asyncResult) +102
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
  System.Web.Mvc.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__18(IAsyncResult asyncResult) +14
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
  System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
  System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
  System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__3(IAsyncResult asyncResult) +25
  System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +23
  System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9690172
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>
  
  <p>I have tried to insert a breakpoint at the line with the Linq query but it seems to throw it already on the method with the method name. </p>
  
  <p><em>What is the problem?</em></p>