Exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

asp.net c# entity-framework exception list

Question

I get the below excption:

Exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code

Additional information: The sequence does not contain Elements.

at line

var kategoria = db.Kategorie.Include("Kursy").Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper()).Single();

My KursyController.cs:

using SklepInternetowy1.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace SklepInternetowy1.Controllers
{
public class KursyController : Controller
{

    private KursyContext db = new KursyContext();

    // GET: Kursy
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Lista(string nazwaKategori)
    {
        var kategoria = db.Kategorie.Include("Kursy").Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper()).Single();
        var kursy = kategoria.Kursy.ToList();

        return View(kursy);
        //return View();
    }
    public ActionResult Szczegoly(string id)
    {
        var kurs = db.Kursy.ToList();
        return View(kurs);
        //return View();
    }

    [ChildActionOnly]
    public ActionResult KetagorieMenu()
    {
        var kategorie = db.Kategorie.ToList();
        return PartialView("_KategorieMenu", kategorie);
    }
}
}

My Lista.cs

@{
ViewBag.Title = "Lista";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="strona">

@Html.Action("KetagorieMenu")



<img src="../Content/Images/dommaly.png" width="5%" height="5%" />
<a href="link">
    Start > ASP.NET
</a>

@Html.Partial("_KursyList")

My _KategorieMenu.cshtml:

@model IEnumerable<SklepInternetowy1.Models.Kategoria>
@using SklepInternetowy1.Infrastructure

<tabela>
<wiersz>

    @foreach (var kategoria in Model)
        {
        <komorka>
            <a href="@Url.Action("Lista","Kursy", new { nazwaKategori = kategoria.NazwaKategorii.ToString() })"
               style="background-image: url(@Url.IkonyKategoriiSciezka(kategoria.NazwaPlikuIkony)); ">
                @kategoria.NazwaKategorii
            </a>
        </komorka>
    }
</wiersz>

My _KursyList.cshtml:

@model IEnumerable<SklepInternetowy1.Models.Kurs>
@using SklepInternetowy1.Infrastructure


<div id="zawartosc">
<tabela>
    <wiersz>
        @foreach (var kurs in Model)
        {

            <komorka>
                <a class="dodajDoKoszykaLink" href="@Url.Action("DodajDoKoszyka","Koszyk",new {id = kurs.KursID})">
                </a>
                @if (kurs.Bestseller)
            {

            }

                <a class="kursSzczegolyLink" href="@Url.Action("Szczegoly","Kursy",new {id =kurs.KursID})">

                    <img class="kurs" src="@Url.ObrazkiSciezka(kurs.NazwaPlikuObrazka)" width="66%" height="66%" />
                    <h1>
                        @kurs.TytulKursu - @kurs.AutorKursu
                    </h1>
                </a>
            </komorka>
        }
    </wiersz>
</tabela>

How to solve this problem?

1
0
1/28/2017 1:00:56 PM

Accepted Answer

In this line:

var kategoria = db.Kategorie
                  .Include("Kursy")
                  .Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper())
                  .Single();

You're calling .Single(), which means you're asserting that there is always exactly one record returned in the result. The error is telling you that no records were returned in the result, so the call to .Single() fails.

Since it's possible for no records to be returned in the result, instead use .SingleOrDefault() and check the result for null. Something like this:

var kategoria = db.Kategorie
                  .Include("Kursy")
                  .Where(k => k.NazwaKategorii.ToUpper() == nazwaKategori.ToUpper())
                  .SingleOrDefault();
if (kategoria == null)
{
    // no record was found.
    // handle that situation in whatever way makes sense for your system.
}

If it's also possible that multiple records can be returned then you'd need to check for that as well, since both .Single() and .SingleOrDefault() will throw an error for multiple results (as their names imply, really). Things like .FirstOrDefault() or additional .Where() clauses, etc. can be helpful to find the record you want in that case.

4
1/28/2017 1:15:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow