0

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?

0

1 Answer 1

6

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.

9
  • 1
    Thanks @Chris, I was just mid-edit after noticing that myself :)
    – David
    Jan 28, 2017 at 13:15
  • if I use .SingleOrDefault() it in a line var kursy = kategoria.Kursy.ToList(); I have communicat: An exception of type 'System.NullReferenceException' occurred in SklepInternetowy1.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.
    – Lukasz
    Jan 28, 2017 at 13:15
  • @Lukasz: Read the rest of the answer. You still need to handle the situation where the result is null, which is the root of the problem in the first place.
    – David
    Jan 28, 2017 at 13:16
  • I am a beginner with programming c #, I use the course: link I did the same as in the video but it does not work. Could you see the viedo and help me?
    – Lukasz
    Jan 28, 2017 at 13:27
  • @Lukasz: There really aren't many ways to explain this. Your code is looking for a record in the data that doesn't exist. So there is no result. You can't use the result when there is no result. So what do you want your code to do when it doesn't find the record it's looking for? You would write code to do that, whatever that is.
    – David
    Jan 28, 2017 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.