tags:

views:

20

answers:

2

I m trying to write a LINQ 2 SQL query wuth left our joins.But when executing i am getting the below exception

The null value cannot be assigned to a member with type System.Byte which is a non-nullable value type

My LINQ 2 SQL Query is here

 Dim items = (From i In oRecelDB.IV00200s _
                Group Join c In oRecelDB.MDS_CONTAINERs _
                    On c.CONTAINERBIN Equals i.BIN Into ic = Group From x In ic.DefaultIfEmpty() _
                Group Join sop In oRecelDB.SOP10201s _
                     On i.SERLNMBR Equals sop.SERLTNUM And i.ITEMNMBR Equals sop.ITEMNMBR _
                     Into os = Group From y In os.DefaultIfEmpty() _
                Group Join iv1 In oRecelDB.IV10002s _
                     On iv1.SERLTNUM Equals i.SERLNMBR And iv1.ITEMNMBR Equals i.ITEMNMBR _
                     Into iv2s = Group From z In iv2s.DefaultIfEmpty() _
                 Where i.SERLNMBR = "2323"  _
                 Select Bin = i.BIN, i.ITEMNMBR, i.LOCNCODE, i.DATERECD, i.SERLNMBR, i.SERLNSLD _
                , YPosted = y.POSTED).ToList()

I understand that the value inside the y.POSTED is coming as null might be the reason.How can i handle that ? Any thoughts ? Thanks in advace

A: 

One possibility I can think of is that when you generated your LINQ context class and object classes you had a database field which was set to NOT NULL.Later on you changed the database field to Null and some records contain null values. You may have to look at the code behind file of you dbml (LINQ) file.Nullable fields like POSTED should be declared like

private System.Nullable<Byte> Posted.

If the field is not defined as nullable you can change it to nullable or regenerate your dbml files and your problem is fixed.

josephj1989
A: 

The problem is that the SQL generated for that query is returning a null value for that column, either due to a left join or a subquery that is returning null. C# is inferring the byte type because the column of the table is not nullable, but the column of the result is nullable.

Fix this by doing

   YPosted = (byte?)y.POSTED
Mike