Reputation: 49
I have the following code. I'm getting error:
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
var productItem = from sales
in db.Sales_inv_details
select new {
sales.Sales_Invoices.Customer.Cust_Id,
sales.Sales_Invoices.Customer.Customer_Name,
sales.Sales_Invoices.Employe.Emp_Name,
sales.Sales_Invoices.Employe.Emp_code,
sales.Sales_Invoices.Date_invoice,
sales.Item.Item_ID,
sales.Qty,
sales.Qty_Price,
sales.Item.Item_Name,
sales.Item.Item_Code,
Unit_Measure = sales.Item.TBL_Unit_Measure.Code,
sales.Sales_Invoices.Descrption,
sales.disc };
productItem = productItem
.Where(x => (x.Date_invoice >= model.Fromdate & x.Date_invoice <= (model.Todate)));
productItem = productItem
.Where(x => (x.Emp_code == model.Emp_code));
Upvotes: 3
Views: 2940
Reputation: 13
Try to handle the null values generated /existing null values by use of
Upvotes: 0
Reputation: 205579
It's hard to tell without having the entity model, but this exception usually happens when the query involves some left outer join
, so although LINQ to Entities does not generate NRE if you don't include null
checks for the right (optional) side, it requires you to promote non nullable value types into nullable inside the projections.
Check some of the following relations: sales.Sales_Invoices
, sales.Sales_Invoices.Customer
, sales.Sales_Invoices.Employe
, sales.Item
, sales.Item.TBL_Unit_Measure
. If some of the right end properties are optional, make sure to include cast to the corresponding nullable type in the projection, i.e. if you have int
property, use = (int?)..
in the select clause (or C#6 ?.
operator).
P.S. The exception message indicates Int32
, so concentrate on int
columns.
Upvotes: 1
Reputation: 1062600
It sounds like your model defines one of the properties as non-nullable, but the database has the corresponding column as nullable, and has a null value in it. When the generated reader attempts to populate the model, BOOM. Unfortunately, we can't tell you which property, and it doesn't need to even be one of those mentioned in that code, since it could be the FrobNumber
property of whatever Sales_inv_details
represents.
You need to check your model very carefully. Start with the properties / columns on whatever Sales_inv_details
represents. When you have found a mismatch: mark it as nullable, and retry.
Upvotes: 5
Reputation: 1356
One of the properties on one of the Entity classes is defined as int
(System.Int32
), but in the database is a null
value. Since int
cannot be null
this error comes up.
To fix this either make sure the database query returns no null values, or change your property definition to a nullable integer (int?
or Nullable<int>
).
To find the respective records in your Database try re-creating the query in SQL Management Studio (i.e. setting the same "WHERE filters"), and check the results for empty columns that should contain integer values.
Upvotes: 2
Reputation: 730
Looks like wrong mapping db fields to the model.
Probably you have int
property in your model, but int?
in database
Upvotes: 1