Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

Marco Dinatsoli

This is my code

var finalResults = (from r in results.AsEnumerable()
                    where r.Field<DateTime>("PrintTime") is DBNull

where PrintTime is a column in my Sql Server 2008 r2 database, its type is datetime and it is nullable

I got this exception:

Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.

Could you help please?

Tim Schmelter

DataRow.Field supports nullable types, so use DateTime? instead of DateTime:

var finalResults = from r in results.AsEnumerable()
                   let printTime = r.Field<DateTime?>("PrintTime")
                   where !printTime.HasValue
                   select r;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

From Dev

Cannot cast DBNull.Value to type 'System.DateTime', Please use nullable types

From Dev

SetField cannot cast DBNull.Value to System.Int32

From Dev

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

From Dev

Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

From Dev

[InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.]

From Dev

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]' when record does exist

From Dev

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'

From Dev

Cannot convert source type system.nullable to target type int

From Dev

Bootstrap Datepicker: Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

From Dev

LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type

From Dev

How to cast an int into a nullable type?

From Dev

How to cast an int into a nullable type?

From Dev

The specified cast from a materialized 'System.Byte[]' type to a nullable 'System.Byte' type is not valid

From Dev

Stored procedure :The specified cast from a materialized 'System.String' type to a nullable 'System.Single' type is not valid

From Dev

'System.Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

From Dev

Cannot convert source type to System.Nullable<double>

From Dev

asp.net error: Cannot implicitly convert type 'object' to 'System.DateTime/Boolean'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot convert value "Mon, 02 Nov 2020 06:39:24 PST" to type "System.DateTime"

From Dev

The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid

From Dev

The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid

From Dev

Unable to cast object of type WhereSelectEnumerableIterator`2[Models.AddonBatch,System.DateTime] to type System.IConvertible

From Dev

Unable to cast object of type 'System.DateTime' to type 'System.String'. c#

From Dev

getting error - Object cannot be cast from DBNull to other types.while editing status column whose data type is bit

From Dev

Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

From Dev

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

From Dev

Working with Time Type in Fluent Nhibernate generates exception "Unable to cast object of type 'System.DateTime' to type 'NHibernate.Type.TimeType"

From Dev

DBNull From DataSet Cast to Nullable Constructor

From Dev

DBNull From DataSet Cast to Nullable Constructor

Related Related

  1. 1

    Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

  2. 2

    Cannot cast DBNull.Value to type 'System.DateTime', Please use nullable types

  3. 3

    SetField cannot cast DBNull.Value to System.Int32

  4. 4

    Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

  5. 5

    Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type

  6. 6

    [InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.]

  7. 7

    Unable to cast object of type 'System.DBNull' to type 'System.Byte[]' when record does exist

  8. 8

    Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'

  9. 9

    Cannot convert source type system.nullable to target type int

  10. 10

    Bootstrap Datepicker: Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

  11. 11

    LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type

  12. 12

    How to cast an int into a nullable type?

  13. 13

    How to cast an int into a nullable type?

  14. 14

    The specified cast from a materialized 'System.Byte[]' type to a nullable 'System.Byte' type is not valid

  15. 15

    Stored procedure :The specified cast from a materialized 'System.String' type to a nullable 'System.Single' type is not valid

  16. 16

    'System.Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

  17. 17

    Cannot convert source type to System.Nullable<double>

  18. 18

    asp.net error: Cannot implicitly convert type 'object' to 'System.DateTime/Boolean'. An explicit conversion exists (are you missing a cast?)

  19. 19

    Cannot convert value "Mon, 02 Nov 2020 06:39:24 PST" to type "System.DateTime"

  20. 20

    The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid

  21. 21

    The specified cast from a materialized 'System.DateTime' type to the 'System.String' type is not valid

  22. 22

    Unable to cast object of type WhereSelectEnumerableIterator`2[Models.AddonBatch,System.DateTime] to type System.IConvertible

  23. 23

    Unable to cast object of type 'System.DateTime' to type 'System.String'. c#

  24. 24

    getting error - Object cannot be cast from DBNull to other types.while editing status column whose data type is bit

  25. 25

    Boxed nullable underlying type can be cast to enum but boxed enum type can't be cast to nullable type

  26. 26

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

  27. 27

    Working with Time Type in Fluent Nhibernate generates exception "Unable to cast object of type 'System.DateTime' to type 'NHibernate.Type.TimeType"

  28. 28

    DBNull From DataSet Cast to Nullable Constructor

  29. 29

    DBNull From DataSet Cast to Nullable Constructor

HotTag

Archive