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 -