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 6854765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:28+00:00 2026-05-27T01:40:28+00:00

I have exception: The null value cannot be assigned to a member with type

  • 0

I have exception:

The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type. 
Source Error: 
var bd = (from d in baza.hpurs where d.userID == userID && d.date !=null select d.date).Max();

My method:

 public ActionResult Add_hours(int userID)
    {
        var dat = DateTime.Today;

        var bd = (from d in baza.hours where d.userID == userID && d.date !=null select d.date).Max();

        if (bd != dat)
        {
            return View();
        }

     else
    {
        var dd = (from d in baza.hours where d.userID == userID && d.date == dat select d.hoursID).Single();
        return RedirectToAction("hours_is", "Employer", new { HoursID = dd });
    }
}

It works good, but only when I have 1 or more data in hours table for concrete user.
This error is when I want add hours to table where concrete user hasn’t added any hours.
I haven’t any idea how fix it…

  • 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-27T01:40:29+00:00Added an answer on May 27, 2026 at 1:40 am

    This is one of the points where LINQ to SQL is a leaky abstraction. The semantics of the Max() function in C# is to throw an InvalidOperationException if there are no elements in the sequence. The semantics of SQL’s MAX() function is to return NULL. The C# code compiles as if the C# semantics are followed, but the code is never executed as C# code. It is translated into SQL where the SQL semantics rules.

    To handle this, you need to decide what you want when there are no matching elements. If you want a null value, explicitly declare a nullable variable and introduce an extra cast to DateTime? to tell LINQ to SQL that null might be returned.

    DateTime? bd = (from d in baza.hpurs 
                    where d.userID == userID && d.date !=null 
                    select (DateTime?)d.date).Max();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thread exception handler which saves the exception stack trace and should close
I have an exception handler for the type SqlException and I'm looking at the
I have following exception in the foreach loop Unable to cast object of type
I have a wpf page hosted in a Window. But i get Null exception
I have an entity class with a bunch of non-nullable string properties. If I
I have an exception handler . In my asp.net application. It’s written in Global.asax.
I have an exception being thrown from a C# method, that takes a generic
I prefer to have the exception handling logic further up in the call stack,
I am new to Hibernate. I have an exception while running an Hibernate-based application.
I have a general exception handler, Application_error in my global.asax where I'm trying to

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.