vb.net – 无法将’System.Int32’类型的对象强制转换为’System.String’类型

前端之家收集整理的这篇文章主要介绍了vb.net – 无法将’System.Int32’类型的对象强制转换为’System.String’类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我得到一个零星的’无法播放类型’System.Int32’的对象,在下面的代码中输入.SingleorDefault()的’System.String’异常.它工作9/10次,但随机抛出异常.我确保我传递的SettingID没有Null值,并且表中的数据始终存在于settingID中,并且我总是将SettingID作为Integer传递.

任何想法这个代码有什么问题.

这是异常信息:

System.InvalidCastException: Unable to cast object of type ‘System.Int32’ to type ‘System.String’.
at System.Data.sqlClient.sqlBuffer.get_String()
at Read_CPT_Setting(ObjectMaterializer`1 )
at System.Data.Linq.sqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source)
at CPT.Service.SettingLinqProvider.GetSettingFromDBById(Int32 SettingId)

码:

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,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检索数据 – 内部上下文 – 使用SettingID = 142176查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – DBSetting什么都不是

不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 142176的数据输入 – 内部上下文 – 使用SettingID = 142176查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – DBSetting什么都不是

场景2

SettingID = 138145– ENTER – 内部上下文中的异常异常 – 使用SettingID = 138145查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 – – 指定的强制转换无效.

不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 138145的数据 – 内部上下文 – 使用SettingID = 138145查询 – 在SingleOrDefault之前 – – 在SingleOrDefault之后 –

场景3

SettingID = 164638– ENTER – 内部上下文中的异常异常 – 使用SettingID = 164638查询 – 在SingleOrDefault之前 – –Index超出数组范围.

不寻常的对象 – MRDD解决方案 – 没有检索到SETID = 164638的数据输入 – 内部上下文 – 使用SettingID = 164638查询 – 在SingleOrDefault之前 –

我跟进了Micorsoft支持,我们启用了debugdiag跟踪并分析了转储文件但无法到达任何地方.

经过分析后我发现3/4服务器没有最新的框架更新,因此服务器管理员已经为.Net Framework 4更新了具有可靠性更新1的机器,它似乎已经解决了这个问题.没有进行任何代码更改.自更新以来已经过了两周,并且在此代码块中没有看到任何一个例外.

我为此浪费了一个月.弄它.有时它不是代码.

原文链接:https://www.f2er.com/vb/255479.html

猜你在找的VB相关文章