score:9
I'm guessing the issue is right here:
ReleaseYear = (int) shows.ReleaseYear
Why do you have to cast shows.ReleaseYear
to an int
? Is it because it's not already an int
? Maybe it's actually a Nullable<int>
?
An int
can't hold a null
value, and the error is telling you that it's encountered a null
value in the data, so that value can't be cast to an int
.
You'll either need to change your data to not allow null
values for that field, or change your type to a Nullable<int>
(or int?
for short).
score:7
It tells you right in there:
The cast to value type 'Int32' failed because the materialized value is null.
This is the offending line:
ReleaseYear = (int) shows.ReleaseYear,
Check your data and make sure everything has a ReleaseYear, or switch to using int?
.
score:3
If you are getting null values then try using these methods for casting -
Convert.ToInt32(string), This would return 0 if value used for parsing is null.
Int32.TryParse(string, out int), this would return true / false when it could parse / not respectively.
In your program before consuming the parsed value, check whether it is valid or not.
Source: stackoverflow.com
Related Articles
- The cast to value type 'Decimal' failed because the materialized value is null
- LINQ: The cast to value type 'System.Int32' failed because the materialized value is null
- The cast to value type 'Int32' failed because the materialized value is null
- Error: The cast to value type 'System.Int32' failed because the materialized value is null
- .Any causes The cast to value type 'Boolean' failed because the materialized value is null
- Cast to value type 'int32' failed because the materialized value is null
- Linq Max: The cast to value type 'System.Decimal' failed because the materialized value is null
- The cast to value type 'double' failed because the materialized value is null - AVERAGE
- Linq error: The cast to value type 'Int32' failed because the materialized value is null
- The cast to value type 'Int64' failed because the materialized value is null
- The cast to value type 'System.Single' failed because the materialized value is null for stored procedure
- Entity Framework Error - The cast to value type 'System.Int64' failed because the materialized value is null
- Error :the cast to value type "Double" failed because the materialized value is null
- cast to value type 'System.Int32' failed because the materialized value is null
- The cast to value type 'System.Int32' failed because the materialized value is null.
- The cast to value type 'Int32' failed because the materialized value is null. Pls help me how to fixed it
- LINQ: failed because the materialized value is null
- The cast to value type 'Int32' failed null value LINQ
- Cast to value type 'Enum' failed as the materialized value is null. Either the result type's param or query must use nullable type
- Why would a linq .Any() throw "The cast to value type 'Boolean' failed because the materialized"?
- The cast to value type 'System.Single' failed in c# linq
- LINQ, Left Join, throwing exception...failed because the materialized value is null
- The cast to value type 'Int32' failed
- The null value cannot be assigned to a member with type Int32 but strings work
- .NET - Attaching an entity of type failed because another entity of the same type already has the same primary key value
- The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid
- LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type
- The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid
- SQL Trigger is trying to insert a null value but my C# code is passing int 300?
- Unable to create a null constant value of type 'System.Int32[]'.
- Insert data into SQL Server CE for windows phone
- Loading nested relationships using Entity Framework
- LINQ to Objects - Grandparent, Parent, Child Relationship
- How to create an Expression that invokes (or is combined with) another Expression using a closure object as argument?
- asp.net mvc data logic structure
- LINQ - double grouping with elements counting
- Enumerating Collections that are not inherently IEnumerable?
- How to mock SqlQuery
- Expression<Func<T,bool>> - How to Handle Ambiguous Method Signatures?
- Single link query where clause to take only `n` of each type from a single model/table
- Using linq to grab data from my xml c#
- SQL Server Profiler do not trace all Entity Framework 4 queries - issue
- EF Core Implement sql server DATEPART as DbFunction static method
- Am I using LINQ correctly to apply a variable amount of filters?
- C# grouping loses row
- Search from the string array and ignore that exact matched string
- LINQ with specific words not working
- C#: Filling a datatable with query results obtained using LINQ
- LINQ query compary multiple with multiple
- How to dynamically contruct / change a part of LINQ statement