可以为空的对象必须具有值。 VB.NET

时间:2014-06-06 20:46:43

标签: vb.net

我需要帮助。我的程序有问题。这是我的业务逻辑层上的代码。

Function Load_ItemDetails(ByVal ItemID As String) As Items
    Dim objItemEnt As New tblitem
    Dim objitem As New Items
    Try
        Using da = New DataAccess
            objItemEnt = da.Load_ItemDetails(ItemID)
            With objitem
                .ItemCode = objItemEnt.ItemCode
                .ItemName = objItemEnt.ItemName
                .Description = objItemEnt.Description
                .NameofType = objItemEnt.NameofType
                .TypeofPricing = objItemEnt.TypeofPricing
                .OnStock = objItemEnt.OnStock
                .ItemPrice = objItemEnt.ItemPrice
                .DateModified = objItemEnt.DateModified
            End With
            Return objitem
        End Using
    Catch ex As Exception
        Throw
    End Try
End Function

此代码适用于我的数据访问层。

Public Function Load_ItemDetails(ByVal ItemCode As String)
    Dim objitem As New tblitem
    Try
        Using entItem = New DAL.systemdbEntities1
            Dim qryUsers = From p In entItem.tblitems
           Where p.ItemCode = ItemCode
           Select p

            Dim luser As List(Of tblitem) = qryUsers.ToList
            If luser.Count > 0 Then
                Return luser.First
            Else
                Return objitem
            End If
        End Using
    Catch ex As Exception
        Throw
    End Try
End Function`

对于我的表现层:

Private Sub Load_Item_Detail(ByVal ItemCode As String)
    objItem = New Items
    Using objLogic = New LogicalLayer
        objItem = objLogic.Load_ItemDetails(ItemCode)
        With objItem
            Me.ItemCodetxt.Text = .ItemCode
            Me.ItemNametxt.Text = .ItemName
            Me.ItemDesctxt.Text = .Description
            Me.ItemTypetxt.Text = .NameofType
            Me.ItemPricetxt.Text = .TypeofPricing
            Me.ItemOnstocktxt.Text = CStr(.OnStock)
            Me.ItemPricetxt.Text = CStr(.ItemPrice)
            Me.TextBox1.Text = CStr(.DateModified)
            Me.ItemCodetxt.Tag = .ItemCode
        End With
    End Using
End Sub`

在我跑完之后,我收到此错误Nullable object must have a value帮助我。我被卡住了。我不知道该怎么做。我是n层架构的新人。

1 个答案:

答案 0 :(得分:3)

要访问可空对象的值,请使用对象的.Value。这个可空对象还有一个.HasValue布尔值,你可以在询问值之前检查它。更多信息:Nullable Types

 If objItemEnt.OnStock.HasValue Then 
  .OnStock = objItemEnt.OnStock.Value
 End If