0

I am receiving this error on a report page on my asp.net app. I have looked at the other answers to questions asked about this error, but am still unable to understand how to resolve it.

I am receiving the error on this line from my .cshtml page:

@(@Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00").Count() +

Here is the complete table row where this is occurring:

                            <tr>
                                <td width="16%" align="left">Count</td>
                                <td width="18%" align="right">
                                    @(@Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "01").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "02").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "03").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "04").Count() +
                                    @Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "05").Count())
                                </td>
                            </tr>

My .cs is as follows:

public class IndexModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public IndexModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public IEnumerable<Incident> Results { get; set; }

    public void OnGet()
    {
        Results = _db.Incident.ToList();
    }

    public void OnPost(DateTime startdate, DateTime enddate, string sortOrder)
    {
        Results = (from x in _db.Incident where (x.ArrestDate >= startdate) && (x.ArrestDate <= enddate) select x).ToList();

        var startdate1 = startdate.ToShortDateString();
        var enddate1 = enddate.ToShortDateString();

        ViewData["startingparameter"] = $"Starting Arrest Date: {startdate1}";
        ViewData["endingparameter"] = $"Ending Arrest Date: {enddate1}";

    }

}

The model Incident that this is based on has the ArrestTime set as nullable:

        [Display(Name = "Arrest Time")]
        [DataType(DataType.Time)]
        public DateTime? ArrestTime { get; set; }

So, I am not quite sure why this is happening. When I published the app, it was working, but has since stopped and the report page returns a 500 error when trying to run it in production.

I have tried adding an if(@Model.Results.Where(x => x.ArrestTime.HasValue){} and putting the Count expression inside of the {}, but that doesn't work.

Any advice would be much appreciated. Thanks so much.

1 Answer 1

1

If ArrestTime is nullable then that implies that it might not have a value. However, this code assumes that it will always have a value:

Model.Results.Where(x => x.ArrestTime.Value.ToString("HH") == "00")

If it always has a value then don't make it nullable, just use a DateTime. However, if it might not have a value then you would need to check if that value exists before trying to use it. For example:

Model.Results.Where(x => x.ArrestTime.HasValue && x.ArrestTime.Value.ToString("HH") == "00")

or:

Model.Results.Where(x => x.ArrestTime != null && x.ArrestTime.Value.ToString("HH") == "00")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! I am mad at myself for not thinking to just add the .HasValue condition as part of each expression using &&. So simple. Again, thank you.

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.