我有以下代码填充设备对象。当我调用SubmitChanges时,记录已成功添加到表中。但是,当我尝试将EquipmentID保存到属性设置中时,它将重置为零。
SaveProperties(Equipment) 'Save the properties to Equipment object
dcVehicle.SubmitChanges() ' Save to database
EquipmentID = Equipment.EquipmentId ' To update ViewState, etc.
Session.Add("EquipmentID", Equipment.EquipmentId)
'Add record to the Employee Assignment table (link to the Equipment table)
Me.AddEquipmentEmployee(Equipment, CInt(Session("divid")))
dcVehicle.SubmitChanges()
'Add a status record for the piece of Equipment
Me.AddEquipmentStatus(Equipment, CInt(Session("divid")))
dcVehicle.SubmitChanges()
在课堂上我有以下代码。当_Equipment设置为Nothing时,_EquipmentID将从有效整数重置为零。有趣的是,我有两个这个应用程序(车辆和预告片)的其他部分具有完全相同的代码(对象名称本身除外)。这两个其他组件按预期工作。当EquipmentID更改为零时,它会在添加好记录后在Equipment表中创建NULL记录。第二个“SubmitChanges”调用将此NULL记录添加到Equipment并将记录添加到EquipmentAssignment表。
'**********************************************
'* Equipment Section *
'**********************************************
Private _Equipment As Equipment
Private _EquipmentID As Integer = 0
Public Property EquipmentID() As Integer
Get
Integer.TryParse(ViewState("EquipmentID"), _EquipmentID)
Return _EquipmentID
End Get
Set(ByVal value As Integer)
Me.ViewState("EquipmentId") = value
_EquipmentID = value
_Equipment = Nothing
End Set
End Property
Public Property Equipment() As Equipment
Get
If _Equipment Is Nothing Then
_Equipment = dcVehicle.GetEquipment(EquipmentID)
End If
Return _Equipment
End Get
Set(ByVal value As Equipment)
_Equipment = value
If value Is Nothing Then
_EquipmentID = 0
Me.ViewState("EquipmentID") = 0
Else
_EquipmentID = value.EquipmentId
Me.ViewState("EquipmentID") = value.EquipmentId
End If
End Set
End Property
答案 0 :(得分:0)
根据this Meta post,我会将我的评论作为答案发布,因此您可以将问题标记为已解决。
ViewState区分大小写。第二行代码段中的第7行,
Me.ViewState("EquipmentId") = value
应为:
Me.ViewState("EquipmentID") = value