locked
LINQ Select giving The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type. RRS feed

  • Question

  • User-2010267424 posted

    Hi

    I have this LINQ statement:

     

     

    bikersList = (From c In ngBikersDataContext.Reg_Bikers _

    Order By c.BikerID _

    Select New Bikers With { _

    .BikerID = c.BikerID, _

    .F_Name = c.F_Name, _

    .M_Name = c.M_Name, _

    .L_Name = c.L_Name, _

    .MyID = c.MyID, _

    .Site = c.Site, _

    .bk_Building = c.bk_Building, _

    .bk_City = c.bk_City, _

    .bk_Zip = c.bk_Zip, _

    .bk_Phone = c.bk_phone, _

    .email = c.email, _

    .DeptZone = c.DeptZone, _

    .QuartID = c.QuartID, _

    .BikerDays = c.BikerDays, _

    .BikerMiles = c.BikerMiles, _

    .BikerTime = c.BikerTime, _

    .BKLockID = c.BKLockID, _

    .bk_Start_DT = c.bk_Start_DT, _

    .bk_End_DT = c.bk_End_DT, _

    .bk_Quarter = c.bk_Quarter, _

    .bk_Year = c.bk_Year, _

    .bk_Comments = c.bk_Comments, _

    .IsActive = c.IsActive _

    }).ToList()


    When I try to run it I get the error:

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

    BikerID is the Primary Key, is set up to increment by 1 on insert. And there are no Nulls in the column BikerID.

    Please help I am new to LINQ  and this is driving me crazy.

    Thank you

    Saturday, March 26, 2011 8:16 PM

All replies

  • User1224940435 posted

    This is not related to primary key but some of the field in your select has null value in integer that return from DB. So when you try to assign it to

    Not null int type it give you error.

    public class Person

    {
         public int Id { get; set;}
         public int ManagerId { get; set;}

    }

    Suppose above is your class and  if you select query against db in that it is possible that MaangerId null in some case ( ID is primary key so it is not null)

    so when you do

    from c in db.....

    select new Person{
       Id=  c.Id,
       ManagerId = c.ManagerId ( If null then throw errro so use ) c.ManagerId.GetDefaultValue()

     

    }

     

    or make ManagerId nullable in Person class.

    Sunday, March 27, 2011 1:56 AM
  • User-2010267424 posted

    Thank you for the response.

    I see what you mean. BUT, I am using LINQ to SQL Class and inside the designer I have

    Private _Site As System.Nullable(Of Interger)

    Isn't that suppose to make it ok for _Site to have Nulls?

    Now in my class that has

    Private m_Site as Interger

    Public Property Site() As Interger

        Get

              Return m_Site

        End Get

        Set(ByVal value As Interger)

               m_Site = value

        End Set

    End Property

     

    Do I have to make a change there?

    Again thank you for the help. Trying to learn this and you are a big help.

    Matt

    Sunday, March 27, 2011 1:42 PM
  • User1224940435 posted

    Private m_Site as System.Nullable(Of Interger)

    Public Property Site() As System.Nullable(Of Interger)

        Get

              Return m_Site

        End Get

        Set(ByVal value As System.Nullable(Of Interger))

               m_Site = value

        End Set

    End Property

     Also tell me how are you using this in your query ? Means when you getah and assign to your class.

     

    Monday, March 28, 2011 12:41 AM
  • User-2010267424 posted

    I am using an ObjectDataaSource to display the data in a gridview.

    Thanks again for the help

    Tuesday, March 29, 2011 11:29 AM