Unable to cast object of type 'System.Int32' to type 'System.String'
Solution 1
I have followed up with Micorsoft Support and we have enabled debugdiag trace and analyze dump files but could not get anywhere.
After analyzing a lot i found out that 3/4 servers has no latest framework updates so the Server Admins have updated the machines with Reliability Update 1 for .Net Framework 4 and it seems to have resolved the issue. No code changes has been done. It has been two weeks since the update and did not see a single exception in this code block.
I wasted a month just for this. Darn it. Sometimes it is just not the code.
Solution 2
Instead of using ToString()
you have to use Convert.ToString()
Hope it will solve your problem.
ravi
Updated on July 04, 2022Comments
-
ravi almost 2 years
I'm getting a sporadic 'Unable to cast object of type 'System.Int32' to type 'System.String' Exceptions at .SingleorDefault() in the below code. It works 9/10 times but randomly it throws an exception. I made sure that SettingID that i'm passing has no Null Values and the data in the table always exists for the settingID, and i'm always passing SettingID as Integer.
Any ideas what is wrong with this code.
Here is the Exception Info:
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)CODE:
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
I've updated the above code with some logging in place after each line.
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
Remember: DB has all the rows for all the settingIDs mentioned in below messages.
Results:
Scenario 1:
Unusual Object - No Data Retrieved for SettingID=142176 ENTER - Inside Context - Querying With SettingID = 142176 -- Before SingleOrDefault -- -- After SingleOrDefault -- - DBSetting Is Nothing
Unusual Object - MRDD Solutions - No Data Retrieved for SettingID=142176 ENTER - Inside Context - Querying With SettingID = 142176 -- Before SingleOrDefault -- -- After SingleOrDefault -- - DBSetting Is Nothing
Scenario 2
Unusual Exception for SettingID=138145-- ENTER - Inside Context - Querying With SettingID = 138145 -- Before SingleOrDefault -- -- After SingleOrDefault -- --Specified cast is not valid.
Unusual Object - MRDD Solutions - No Data Retrieved for SettingID=138145 ENTER - Inside Context - Querying With SettingID = 138145 -- Before SingleOrDefault -- -- After SingleOrDefault --
Scenario 3
Unusual Exception for SettingID=164638-- ENTER - Inside Context - Querying With SettingID = 164638 -- Before SingleOrDefault -- --Index was outside the bounds of the array.
Unusual Object - MRDD Solutions - No Data Retrieved for SettingID=164638 ENTER - Inside Context - Querying With SettingID = 164638 -- Before SingleOrDefault --
-
ravi almost 12 yearsSettingID is the primary key so it will return only one value. I also tried with .SingleOrDefault as well. The error seems to recurr.
-
ravi almost 12 yearsI did try that too but it is of no use