【发布时间】:2016-04-04 22:31:40
【问题描述】:
我在下面的实体类中有一个代码。它在 Entity.Designer.vb 中抛出异常,说 Table Patient 中的值 Amount 是 DbNull。
If _patientDetails.ID > 0 Then
If _patientDetails.Amount = EntityEnums.Patient.Existing
Then
_patientDetails.SetAmountNull()
End If
Catch ex As Exception
_patientDetails.SetAmountNull()
End Try
End If
它进入如下的“返回”行并抛出异常。
Public Property Amount() As Integer
Get
Try
Return CType(Me(Me.tablePatient.AmountIDColumn),Integer)
Catch e As Global.System.InvalidCastException
Throw New Global.System.Data.StrongTypingException("The value for column 'AmountIDColumn' in table 'Patient' is D"& _
"BNull.", e)
End Try
End Get
Set
Me(Me.tablePatient.AmountIDColumn) = value
End Set
End Property
异常详情是
Microsoft.VisualBasic.dll 中发生了“System.InvalidCastException”类型的第一次机会异常
附加信息:从“DBNull”类型到“Integer”类型的转换无效。
【问题讨论】:
-
您的第一个块似乎不完整/搞砸了。
-
@Plutonix:嗨,Plutonix,我只是更改了一些名称,仅此而已。这是 dbnull 到整数转换的错误。他们到处都有这个,但我担心只有在这个地方它会抛出错误,因为我使用了这个专栏。有什么建议可以解决吗?
-
@Plutonix:已更正
-
没有。
If _patientDetails.Amount的 If 块格式不正确。行不以Then开头 -
@Plutonix:这就是我的项目中的内容。正如您所怀疑的那样,它正是它引发异常的位置,但是当控件进入“第一个 If 块”时。显然它正在尝试访问该属性,并且在该属性中,它会引发异常。
标签: .net vb.net exception casting dbnull