locked
The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type. RRS feed

  • Question

  • I have added the new column to the sql database with datatype as datetime and set to allow null. when i run my solution i get the  exception at the line

    return dataContext.Employee_Masters.Single(e => e.Emp_No == Emp_No);

    as

    The null value cannot be assigned to a member with type System.DateTime which is a non-nullable value type.

       public Employee_Master GetData_By_Emp_No(int Emp_No)
        {
            
              return dataContext.Employee_Masters.Single(e => e.Emp_No == Emp_No);
         }

    pls tell me how to write that code so that the error get removed.

    Thursday, July 5, 2012 10:33 AM

Answers

  • Hi,

    not sure that I understood you correctly. It seems that you changed the database but didn't change the definition of your entities to match the database change.

    If the field inside the database is nullable, you need to make it nullable inside the entity, too. So the type will be DateTime? (which is short for Nullable<DateTime>) and if you use the EF of ADO.Net, you could simpyl do it inside the designer by setting the property "Nullable" to true.

    With kind regards,

    Konrad

    Thursday, July 5, 2012 10:49 AM

All replies

  • Hi,

    not sure that I understood you correctly. It seems that you changed the database but didn't change the definition of your entities to match the database change.

    If the field inside the database is nullable, you need to make it nullable inside the entity, too. So the type will be DateTime? (which is short for Nullable<DateTime>) and if you use the EF of ADO.Net, you could simpyl do it inside the designer by setting the property "Nullable" to true.

    With kind regards,

    Konrad

    Thursday, July 5, 2012 10:49 AM
  • Thanks Sir, its working now properly by setting the property "Nullable" to true in designer.
    Friday, July 6, 2012 4:44 AM