tags:

views:

37

answers:

1

Dear All,

Please help me When i execute this linq to sql command its given an error "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type."

  1. 0 Records in my Commotidy Table
  2. I want generate manual number and get the last CommodityCode

My Code as follow,

Dim QRecordCount = From RC In cntxtCommodity.CommodityMasters _
                                 Where RC.CommodityCode <> 0 _
                                Select RC.CommodityCode

                LastCommodityCode = QRecordCount.Max() + 1

VB.NET 2008

Suhaib

A: 

Hi there.

I've tried to replicate your code with this example:

Module Module1

    Sub Main()

        Dim cntxtCommodity As New List(Of CommodityMasters)

        cntxtCommodity.Add(New CommodityMasters With {.CommodityCode = 34})
        cntxtCommodity.Add(New CommodityMasters With {.CommodityCode = 1})
        cntxtCommodity.Add(New CommodityMasters With {.CommodityCode = 234})
        cntxtCommodity.Add(New CommodityMasters())

        Dim LastCommodityCode = cntxtCommodity.Where(Function(rc) rc.CommodityCode.HasValue AndAlso rc.CommodityCode > 0).Max(Function(rc) rc.CommodityCode)

    End Sub

End Module

Public Class CommodityMasters

    Public CommodityCode As Nullable(Of Integer)

End Class

Basically, the above is supposed to deal with nullable integers (CommodityCode) which should stop you getting exceptions on that part. I'm not 100% sure if it solves all your problems though, with no records in the database. Try it.

Cheers. Jas.

Jason Evans
Thanks for your kind Jason, still it couldn't shortout
Suhaibnm
@Suhaibnm - Can you try declaring the variable `LastCommodityCode` as `Nullable(Of Integer)` before calling the line `LastCommodityCode = QRecordCount.Max() + 1`? Possibly you have `LastCommodityCode` declared as an `Integer`.
Jason Evans
i used above type but error appear The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
Suhaibnm