我在下面的代码中出现了一个零星的“无法将类型为'System.Int32'的对象强制转换为类型为'System.String'的异常。它可以工作9/10次,但会随机抛出一个异常。我确保我传递的SettingID没有空值,并且表中的数据始终存在于settingID中,并且我始终将SettingID传递为Integer。
任何想法这段代码有什么问题。
这是异常信息:
代码:
Public Function GetSettingFromDBById(ByVal SettingId As Integer) As ReturnObject(Of Model.ISettingBase)
Dim retObj As New ReturnObject(Of Model.ISettingBase)
Dim dbSetting As CPT_Setting
Try
Dim _cptDB As New CPT.Data.CPTLinqRepository.DB(_connString)
Using _cptDB
dbSetting = (From s In context.CPT_Settings _
Where s.SettingId = settingId _
Select s).SingleOrDefault
If dbSetting IsNot Nothing Then
retObj.ReturnValue = Mapping.Setting.MapDBToModel(dbSetting)
End If
End Using
Catch ex As Exception
retObj.ReturnValue = Nothing
retObj.AddErrorMessage("Error returning the site: " & ex.Message)
_log.Error(ex.Message, _userId, ex)
End Try
If retObj.ReturnValue Is Nothing Then
retObj.AddErrorMessage("Site Not Found.")
End If
Return retObj
End Function
我已经在每行之后添加了一些日志记录,从而更新了上面的代码。
Public Function GetSettingFromDBById(ByVal SettingId As Integer) As ReturnObject(Of Model.ISettingBase)
Dim retObj As New ReturnObject(Of Model.ISettingBase)
Dim dbSetting As CPT_Setting
Dim SettingsList As New List(Of CPT_Setting)
Dim errStr As String = " ENTER "
Try
Dim _cptDB As New CPT.Data.CPTLinqRepository.DB(_connString)
Using _cptDB
errStr &= " - Inside Context "
If _cptDB Is Nothing Then
errStr &= " - With Context is Nothing "
_log.Error("Unusual Object - Unable to create connection object - Object is NOTHING", _userId)
End If
If System.DBNull.Value.Equals(_cptDB) Then
errStr &= " - With Context is NULL "
_log.Error("Unusual Object - Unable to create connection object - Object is NULL", _userId)
End If
errStr &= " - Querying With SettingID = " & SettingId.ToString()
Dim dbSettingTemp = (From s In context.CPT_Settings _
Where s.SettingId = settingId _
Select s)
If dbSettingTemp Is Nothing Then
errStr &= " -- Nothing is returned from DB - Object is NOTHING -- "
_log.Error(errStr, _userId)
End If
If System.DBNull.Value.Equals(dbSettingTemp) Then
errStr &= " -- Nothing is returned from DB - Object is NULL -- "
_log.Error(errStr, _userId)
End If
errStr &= " -- Before SingleOrDefault -- "
dbSetting = dbSettingTemp.SingleOrDefault
errStr &= " -- After SingleOrDefault -- "
If dbSetting IsNot Nothing Then
If System.DBNull.Value.Equals(dbSetting) Then
errStr &= " - NULL OBJECT RETURNED - Before Mapping "
_log.Error("Unusual Exception - NULL OBJECT RETURNED " & errStr, _userId)
End If
retObj.ReturnValue = Mapping.Setting.MapDBToModel(dbSetting)
errStr &= " - After Mapping With SettingID=" & dbSetting.SettingId.ToString() & " SettingName=" & dbSetting.SettingName.ToString() & " StartDate=" & dbSetting.StartDate.ToShortDateString() & " EndDate=" & dbSetting.EndDate.ToShortDateString()
Else
errStr &= " - DBSetting Is Nothing "
_log.Error("Unusual Object - No Data Retrieved for SettingID=" & SettingId.ToString() & " " & errStr, _userId)
End If
End Using
Catch ex As Exception
retObj.ReturnValue = Nothing
retObj.AddErrorMessage("Error returning the site: " & ex.Message)
_log.Error("Unusual Exception for SettingID=" & SettingId.ToString() & "--" & errStr & "--" & ex.Message, _userId, ex)
End Try
If retObj.ReturnValue Is Nothing Then
retObj.AddErrorMessage("Site Not Found.")
_log.Info("Unusual Object - MRDD Solutions - No Data Retrieved for SettingID=" & SettingId.ToString() & " " & errStr, _userId)
End If
Return retObj
End Function
请记住:DB具有以下消息中提到的所有settingID的所有行。
结果:
方案1:
异常的对象-没有为SettingID = 142176检索到的数据ENTER-内部上下文-使用SettingID = 142176进行查询-在SingleOrDefault之前--在SingleOrDefault之后--DBSetting没有
异常的对象-MRDD解决方案-设置ID = 142176的数据未获取ENTER-内部上下文-设置ID = 142176的查询-SingleOrDefault之前--SingleOrDefault之后-DBSetting没有
方案2
SettingID = 138145的异常异常-ENTER-内部上下文-SettingID = 138145的查询-SingleOrDefault之前--SingleOrDefault之后-指定的转换无效。
异常对象-MRDD解决方案-设置ID = 138145时未检索任何数据ENTER-内部上下文-设置ID = 138145的查询-在SingleOrDefault之前--在SingleOrDefault之后-
方案3
SettingID = 164638的异常异常-ENTER-内部上下文-SettingID = 164638进行查询-SingleOrDefault之前---Index超出数组范围。
异常对象-MRDD解决方案-设置ID = 164638的数据未获取ENTER-内部上下文-设置ID = 164638的查询-在SingleOrDefault之前-
最佳答案
我对Micorsoft支持进行了跟踪,我们启用了debugdiag跟踪功能并分析了转储文件,但无法到达任何位置。
经过大量分析,我发现3/4服务器没有最新的框架更新,因此服务器管理员已使用更新了.Net Framework 4 的可靠性更新1的计算机,似乎已解决了该问题。没有代码更改已完成。自更新以来已经两周了,在此代码块中未看到任何异常。
我为此只浪费了一个月。真是的有时,这不是代码。
关于vb.net - 无法将 'System.Int32'类型的对象强制转换为 'System.String'类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9794648/