Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:29:01+00:00 2026-05-27T22:29:01+00:00

I wanted to create my own basic online shop so I had a look

  • 0

I wanted to create my own basic online shop so I had a look around and in the end chose to modify the MVC Music Store tutorial in to something tailored to my needs.

I am still in the early stages and so far, my version is very similar to the tutorial version with the exception of the tables and associated classes. Where the tutorial has Genres, Albums and Artists – I have a more simple Product and Category and I have adapted my project accordingly but I am getting an error.

It only happens in the StoreController\Browse section. When I try and view all items in a specific category, I get a System.InvalidOperationException “Sequence contains no elements”

The code used in the controller is:

public ActionResult Browse(string cat)
{
var categoryModel = storeDB.Categories.Include("Products")
    .Single(g => g.CatName == cat);

return View(categoryModel);
}

And the view looks like this:

@model TPATK_shop.Models.Category

@{
    ViewBag.Title = "Browse";
}

<h2>Browsing Category: @Model.CatName</h2>

<ul>
    @foreach (var product in Model.Products)
    {
        <li>
            @product.ProdName
        </li>
    }
</ul>

I have all of my classes and entity set up as follows:

namespace TPATK_shop.Models
{
    public partial class Category
    {
        public int CategoryId { get; set; }
        public string CatName { get; set; }
        public string Description { get; set; }

        public List<Product> Products { get; set; }
    }
}

namespace TPATK_shop.Models
{
    public class Product
    {
        public int ProductId { get; set; }
        public int CategoryId { get; set; }
        public string ProdName { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string ImageURL { get; set; }

        public Category Category { get; set; }
    }
}

namespace TPATK_shop.Models
{
    public class TPATKStoreEntities : DbContext
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }
    }
}

I have been researching the issue and I cant seem to find a solution. I have tried singleordefault but I still get the error.

I have confirmed that all the data is present in the DB and the keys are correct.

When my project fires up, an entry in the global.asax fires the following code to populate the DB (for test purposes – as detailed in the walkthrough)

namespace TPATK_shop.Models
{
    public class SampleData :  DropCreateDatabaseAlways<TPATKStoreEntities>
    //public class SampleData : DropCreateDatabaseIfModelChanges<TPATKStoreEntities>
    {
        protected override void Seed(TPATKStoreEntities context)
        {
            var categories = new List<Category>
            {
                new Category { CatName = "50's" , Description = "50's stuff" },
                new Category { CatName = "Weddings" , Description = "Wedding stuff" },
                new Category { CatName = "Dribble Bibs" , Description = "Dribble bibs for kids" },
                new Category { CatName = "Xmas" , Description = "Xmas stuff" },
            };

            new List<Product>
            {
                new Product { ProdName = "Headband 1", Description = "Des for headband 1", Category = categories.Single(g => g.CatName == "50's"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 2", Description = "Des for headband 2", Category = categories.Single(g => g.CatName == "Weddings"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 3", Description = "Des for headband 3", Category = categories.Single(g => g.CatName == "Dribble Bibs"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 4", Description = "Des for headband 4", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
                new Product { ProdName = "Headband 4", Description = "Des for headband 5", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
            }.ForEach(a => context.Products.Add(a));
        }
    }
}

Can anyone see why I am getting the error? Any help would be greatly received.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:29:02+00:00Added an answer on May 27, 2026 at 10:29 pm

    That is because the supplied cat has not matching record in the database. Use SingleOrDefault method to retrieve the entity, it will return null if there wasn’t a record.

    public ActionResult Browse(string cat)
    {
        var categoryModel = storeDB.Catagories.Include("Products")
            .SingleOrDefault(g => g.CatName == cat);
    
        if (categoryModel == null)
           return RedirectToAction("Index");
    
        return View(categoryModel);
    }
    

    It maybe the case that MVC can not bind a value to the cat parameter and is set to null. So check whether you are passing a query string argument named cat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create my own Python exception class, like this: class MyException(BaseException): def
If I wanted to create my own relational database with a modern language to
I have a problem over here. You see, I wanted to create my own
I wanted to create my own template for the admin-generator, so I've followed a
I just wanted to create my own simple document using the agility pack so
I wanted to create a custom membership provider for my asp.net mvc application, but
I wanted to create smth similar to this one Kansas county map where user
I wanted to create a new property on a table in my model.. Basically
I wanted to create a control with a TextBox and to bind TextBox.Text property
I wanted to create a very simple method that switches between views in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.