1

I am trying to display the result from the database to the user on the front end but I keep getting the following exception:

InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'

and

System.InvalidOperationException: 'An exception occurred while reading a database value for property 'CList.CourseCategory'. The expected type was 'LearnAngebot.Models.Courscategory' but the actual value was of type 'System.String'.'

I am lost as to what has gone wrong.

I have a similar code to display the courses and it seems to be working fine. I used the same format for this but keep getting this error.

The C# code:

public class IndexModel : PageModel
{
    public CUser User { get; set; }
    public bool empty { get; set; }

    public readonly IHostingEnvironment _env;
    private readonly CDataContext _context;
    private readonly IUserService _UserService;

    public IndexModel(CFE_CrazyLabContext context, IUserService UserService, IHostingEnvironment hostingEnvironment)
    {
        _env = hostingEnvironment;
        _context = context;
        _UserService = UserService;
        User = UserService.GetUser();
    }

    public IList<CList> ResultList { get; set; }
    public CUser StudentUser { get; set; }
    public CStudent Student { get; set; }

    public void LoadList(CList list)
    {
        StudentUser = _UserService.GetUser(list.StudentUser);
    }
    public void OnGet()
    {
        ResultList = _context.Result.Where(o => EF.Functions.Like(o.StudentUser, User.UserName)).ToList();

    }
}

HTML code:

@if(Model.ResultList.Count == 0)
{
<div>
    <h2> No Result yet </h2>
</div>
}

@if(Model.ResultList.Count > 0)
{
    <div class="Custom-Table Custom-Table-Big">
    <table>
        <tbody>
            <tr id="head">
                <th><a href="index.pgp?">Student UserName</a></th>

                <th><a href="index.php?">Course Name</a></th>

                <th><a href="index.pgp?">Category</a></th>

                <th><a href="index.pgp?">Date</a></th>
            </tr>

            @foreach(var item in Model.ResultList)
            {
                Model.LoadList(item);
                <tr>
                    <td>
                        @item.StudentUser
                    </td>

                    <td>
                        @item.CourseName
                    </td>

                    <td>
                        @item.CourseCategory
                    </td>

                    <td>
                        @item.Date
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>
}

Why exactly am i getting this exception and how do I resolve it? Thanks in advance.

1
  • Shouldn’t be item instead of @item inside loop? Commented Aug 19, 2019 at 8:58

2 Answers 2

2

Please check database Date Datatype and Model Date datatype and match together. If you getting error in any other property please debug using F11 and match from database datatype and model datatype.

Sign up to request clarification or add additional context in comments.

1 Comment

welcome.. Hahaha Developer dates are very confusing :)
0

InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32': This error emerges from an error in your datatype of one of the column/Field in your table(model)

I will advise you to look at your model very well and make sure the type of datatype you are using to save your data because the system will not pinned point the specific field or column for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.