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


Where does the error come from? Is it possible that this is calling d.IsRemarkNull ()? What does this method look like?

May be:



DBNull.Value.Equals(d.Remark)

      

0


a source


maybe this field does not allow null in db, gets the default for it and avoids referencing null values

0


a source







All Articles