Click here to Skip to main content
15,886,753 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I keep getting this
Error: An unhandled exception occurred while processing the request.<br />
SqliteException: SQLite Error 1: 'no such table: Course'.


I am doing ASP.NET Core Razor Pages

What I have tried:

Here is my "Course.cs" file that is under folder named "Models" :
C#
using System;

using System.ComponentModel.DataAnnotations;
namespace CoursesSite.Models
{
    public class Course
    {

        public int ID { get; set; }
        public string Courses { get; set; }
        [DataType(DataType.Date)]
        public DateTime CourseDate { get; set; }
        public string InstructorsName { get; set; }

    }
}





Here is my Index file named "IndexModel.cs":
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace CoursesSite.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }
    }
}



And here is my file "Index.cshtml" under "Courses" folder that is under "Pages" folder:

HTML
@page
@model CoursesSite.Pages.Courses.IndexModel

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>

<p>
    <a asp-page="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Course[0].Courses)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Course[0].CourseDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Course[0].InstructorsName)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model.Course) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Courses)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CourseDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InstructorsName)
            </td>
            <td>
                <a asp-page="./Edit" asp-route-id="@item.ID">Edit</a> |
                <a asp-page="./Details" asp-route-id="@item.ID">Details</a> |
                <a asp-page="./Delete" asp-route-id="@item.ID">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>


Please ask questions if my question dont make any sense.
Posted
Updated 21-Apr-23 10:39am
v2
Comments
Graeme_Grant 21-Apr-23 5:59am    
The Table Course is not found in the database. Have you checked?
PIEBALDconsult 21-Apr-23 15:37pm    
Is it Course? Or Courses?

1 solution

You have a bit of a naming problem that makes it hard to understand what's going on.

In your Course class, you have a property called Courses. In your Razor page, you have a foreach referring to Model.Course:
@foreach (var item in Model.Course) {

The thing is you passed an instance of the Course class to the page straight out of your database EF code. That instance, in the page, is referred to as Model, not Course! The Course class does not have a property called Course. It's called Courses. This is what is causing the error.

But! You have an even bigger problem. Your Course property is a string, not a collection, so you can't even use a foreach on it! You only have a single string value describing all of the courses for an instructor.

Your database model needs to be overhauled to really fix this and do things correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900