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: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.
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?
.
Source: stackoverflow.com
Related Query
- 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
- The given value of type String from the data source cannot be converted to type int of the specified target column
- The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type
More Query from same tag
- how to get value from xml by Linq
- Can a class inherit from a collection and still efficiently interact with LINQ?
- Simplifying Linq expression
- SQL to LINQ for SubQuery Entity Framework
- Username/Password Database Checking
- Easiest way to get a common base class from a collection of types
- Dynamic Linq query In Clause
- How do I .OrderBy() and .Take(x) this LINQ query?
- Combination of grouping by several fields and select latest date?
- LINQ: How to get the Max Id with a group by clause?
- performance issue while binding gridview with huge data
- Average of 3 IEnumerables
- Querying a Single Column with LINQ
- Using Where on Enum Values
- LINQ query structure with if statement?
- Building a LINQ query based on a JSON array
- Finding consecutive rows to group based on arbitrary
- Converting string to int in linq
- Performance Difference between LINQ and Stored Procedures
- LINQ: Most Efficient Where Condition When Selecting a Single Item out of a Million
- LINQ2SQL doesn't return row if checking with null
- How can I order the output of my long LINQ query by a double and then a string?
- How to convert UTC datetime column to local time in datagridview?
- LINQ to JSON - Querying an array
- How can I do this LINQ query on a condition that something not exist in a table?
- Use LINQ instead of two for loops
- AutoMapper throws exception when projecting to nullable enum
- convert foreach to linq expression
- Converting list to json in C#
- Compare two list Items using LINQ