Invalid cast from 'System.Int32' to 'System.Nullable1System.Int32, mscorlib
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This exception usually appears in reflection, data mapping, or generic conversion code that operates on object and Type values at runtime. In ordinary C# assignments, int and int? work together naturally, but runtime conversion APIs do not automatically wrap an int into Nullable<int> the way the compiler does.
That distinction is the whole problem. The language understands nullable conversions, while a generic conversion helper such as Convert.ChangeType generally does not.
Why the Cast Fails
At compile time, this is fine:
The compiler inserts the correct nullable conversion.
But runtime code often looks more like this:
If you then call Convert.ChangeType(raw, targetType), it can fail because Nullable<int> is not treated as an ordinary primitive conversion target.
There is also a boxing detail behind this: when int? has a value, boxing usually boxes the underlying int, not a special nullable wrapper object. So a runtime mapper often sees System.Int32 and then has to decide how to build int? from it.
Convert the Underlying Type First
The usual fix is:
- check whether the target type is nullable
- extract the underlying type with
Nullable.GetUnderlyingType - convert to that underlying type
- return
nullfornullorDBNull.Value
The conversion works because the runtime converts to int, not directly to int?.
Reflection and Property Setters Need the Same Pattern
This exception is common when populating object properties dynamically.
If you skip the nullable-unwrapping step and pass property.PropertyType directly into a generic conversion API, you are likely to hit the exception again.
DBNull.Value Needs Explicit Handling
Database code is another common source of this problem. DBNull.Value is not the same thing as null, and if you treat them as identical only sometimes, nullable mapping bugs follow quickly.
A typed data-reader approach is often safer than broad runtime conversion:
When you know the source schema, direct typed access is simpler, faster, and less fragile than a generalized conversion pipeline.
Prefer Strong Typing When You Can
The deeper lesson is that runtime conversion should live at boundaries, not all through the codebase. If the application can get into strong types early, nullable conversions stop being mysterious because the compiler handles them naturally.
Generic mappers are useful, but they need explicit nullable handling. Otherwise they end up reinventing pieces of the type system badly.
Common Pitfalls
The biggest mistake is calling Convert.ChangeType(value, typeof(int?)) and expecting it to behave like a normal C# assignment. Another is forgetting about DBNull.Value in data-access code. Developers also sometimes overuse generic runtime conversion even when the source schema is known and a typed accessor would be clearer. Finally, a boxed nullable with a value often appears as the underlying non-nullable type, which surprises people who expect runtime type checks to preserve the source syntax.
Summary
- This exception is a runtime conversion issue, not a normal C# assignment issue.
- '
Convert.ChangeTypeusually converts to the underlying type such asint, not directly toint?.' - Unwrap nullable targets with
Nullable.GetUnderlyingTypebefore converting. - Treat
nullandDBNull.Valueexplicitly in mapping code. - Prefer typed access at boundaries when the source schema is already known.

