vb.net 可为空的对象必须有一个值。网络

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24090347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:40:20  来源:igfitidea点击:

Nullable object must have a value. VB.NET

vb.net

提问by Gcode

I need help. I'm having problem in my program. This is my code on my Business logic layer.

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

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

This code is for my data access layer.

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

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`

For my Presentation layer:

对于我的表示层:

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`

and after I run, I get this error Nullable object must have a valuehelp me. I'm stuck. I don't know what to do guys. I new in n tier architecture.

在我跑步后,我得到这个错误Nullable object must have a value帮助。我被困住了。我不知道该怎么办伙计们。我是 n 层架构的新手。

回答by OneFineDay

To access the value of a nullable object you use the .Valueof the object. This nullable object also has a .HasValueboolean you can check before asking for the value. More here: Nullable Types

要访问可为空对象的值,请使用.Value对象的 。这个可为空的对象还有一个.HasValue布尔值,您可以在请求值之前进行检查。更多信息:可空类型

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