views:

340

answers:

2

In my LINQ-to-SQL DAL, I'm getting a "Specified Cast Was Not Valid."

Here is the exact error:

System.InvalidCastException was unhandled by user code   Message="Specified cast is not valid." Source="System.Data.Linq"   StackTrace:
   at System.Data.Linq.IdentityManager.StandardIdentityManager.SingleKeyManager`2.TryCreateKeyFromValues(Object[] values, V& v)
   at System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
   at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
   at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
   at System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
   at System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
   at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   at System.Data.Linq.DataContext.SubmitChanges()
   at DomainModel.Repository.Concrete.SqlClientRepository.InsertClientByUsername(String username, Client clientInfo) in C:\Documents and Settings\Owner\Desktop\CollectionTree v1.1\DomainModel\Repository\Concrete\SqlClientRepository.cs:line 99
   at WebUI.Secure.Controllers.ClientAdminController.EditProfile(Client clientProfile) in C:\Documents and Settings\Owner\Desktop\CollectionTree v1.1\WebUI.Secure\Controllers\ClientAdminController.cs:line 69
   at lambda_method(ExecutionScope , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)   InnerException:

It is happening on my db.SubmitChanges().

Anyway, where in this exception is what types it is having trouble casting? It would help narrow it down if I knew what specific types it was having issues with. I can hover over the exception in Visual Studio but I don't know what properties to look at.

+1  A: 

I occasionally get an InvalidCastException via COM interop and ... I'm SOL? It seems an InvalidCastException in pure IL get the type information, but when it's handled in an interop wrapper conversion it skips that part. I'm guessing in the COM wrappers the problem is due to the fact that the HRESULT for E_NOINTERFACE doesn't contain information about the types involved, and the wrapper has no way to go back and look at the previous item that QueryInterface was looking for. Even if it did, it'd have to go looking for the Guid and hope an interop wrapper for the type was laying around to get meaningful information from it.

If LINQ-to-SQL uses COM interop behind the scenes, there's your answer. :)

280Z28
+1  A: 

You're SOL, at least for .NET < 4.0. I entered a Connect bug report on this ("InvalidCastException Should Display or Contain More Detail"), and they say it's fixed.

I haven't seen the fix yet.

John Saunders