LINQ to Dataset DBNull Task / Null Reference Exception
I have the following LINQ query that always throws an error when my "Remark" column in dtblDetail is null, even if I check if it is NULL.
var varActiveAndUsedElementsWithDetails =
from e in dtblElements
join d in dtblDetails on e.PK equals d.FK into set
from d in set.DefaultIfEmpty()
where (e.ElementActive == true)
select new
{
ElementPK = e.PK,
Remark = d.IsRemarkNull() ? null : d.Remark
};
Error message: "The value for the" Comment "column in the" dtblDetails "table is DBNull." After adding a test for d.IsRemarkNull (), a null reference exception is thrown.
Can you help me?
I have already checked the following websites but didn't find anything useful other than what I should check for DBNULL. But since this doesn't solve my problem.
0
a source to share
3 answers
The problem was that the entire 'd' element was empty. Therefore, calling d.IsRemarkNull () resulted in a null reference exception. The following code fixed the problem:
var varActiveAndUsedElementsWithDetails =
from e in dtblElements
join d in dtblDetails on e.PK equals d.FK into set
from d in set.DefaultIfEmpty()
where (e.ElementActive == true)
select new
{
ElementPK = e.PK,
Remark = d == null? null : (d.IsRemarkNull() ? null : d.Remark)
};
+2
a source to share