Unable to pass object of type "System.Int32" to enter "System.String"

I get the sporadic "Unable to pass an object of type" System.Int32 "to throw" System.String "exceptions in .SingleorDefault () in the code below. It works 9/10 times but randomly throws an exception. I made sure the SettingID parameter The one I pass in is not Null, and the data in the table always exists for the installation identifier, and I always pass the SettingID parameter as Integer.

Any ideas what is wrong with this code.

Here is the exception information:

System.InvalidCastException: it is not possible to cast an object of type "System.Int32" to enter "System.String".
in System.Data.SqlClient.SqlBuffer.get_String ()
in Read_CPT_Setting (ObjectMaterializer`1)
in System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext ()
in System.Linq.Enumerable.SingleOrDefault [TSource] (IEnumerable'1 source)
in System.Linq.Queryable.SingleOrDefault [TSource] (IQueryable`1 source)
on 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 updated the code above with some data entry 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 rows for all parameter identifiers specified in the messages below.

Results:

Scenario 1:

Unusual object - no data received for parameter ID = 142176 ENTER - inside the context - query using parameter ID = 142176 - before SingleOrDefault - - after SingleOrDefault - - DBSetting Nothing

Unusual object - MRDD solutions - No data received for parameter ID = 142176 ENTER - Internal context - Request using parameter ID = 142176 - Before SingleOrDefault - - After SingleOrDefault - - DBSetting Nothing [/ p>

Scenario 2

Unusual exception for SettingID = 138145-- ENTER - Internal context - query using parameter ID = 138145 - before SingleOrDefault - - after SingleOrDefault - - Specify a job is invalid.

Unusual object - MRDD solutions - No data received for parameter ID = 138145 ENTER - Internal context - Request using parameter ID = 138145 - Before SingleOrDefault - - After SingleOrDefault -

Scenario 3

An unusual exception for SettingID = 164638-- ENTER - Internal context - a query using the parameter ID = 164638 - before SingleOrDefault - --Index was outside the array.

Unusual object - MRDD solutions - No data received for parameter IDID = 164638 ENTER - Internal context - Request using parameter ID = 164638 - Before SingleOrDefault -

+8
casting linq linq-to-sql
source share
4 answers

I monitored Micorsoft support, and we turned on debugdiag tracing and analyzed the dump files, but couldn’t get there.

After analyzing a large number, I found out that 3/4 servers do not have the latest frame updates, so the server administrators updated the machines using Reliability Update 1 for .Net Framework 4 and it seems to have solved the problem. There were no code changes. Two weeks have passed since the update and not a single exception was visible in this block of code.

I spent a month on this. Drain it. Sometimes it's just not code.

+3
source share

Instead of using ToString() you need to use Convert.ToString()

Hope this solves your problem.

+1
source share

You must check that

 From s In context.CPT_Settings _ Where s.SettingId = settingId _ Select s 

does not return more than one object. It may never be , but it can be a problem.

0
source share

Use single instead of singleordefault?

So we recently resolved a similar situation at work.

0
source share

All Articles