Neil
Neil

Reputation: 163

Exception has been thrown by the target of an invocation - InvalidCastException in App_Code.dll

I have an asp.net application in which I am receiving the following error message: -

Exception: System.Reflection.TargetInvocationException

Message: Exception has been thrown by the target of an invocation.

Stack Trace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance)
at System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at System.Web.UI.WebControls.FormView.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
at System.Web.UI.WebControls.FormView.EnsureDataBound()
at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
at System.Web.UI.Control.EnsureChildControls()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Control.PreRenderRecursiveInternal()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

In an effort to debug the error I have turned the EnablePartialRendering setting to false but this didn't help and the error was displayed in exactly the same way.

The only clue I have is that in the Output window for Debug I am seeing this message: - 'A first chance exception of type 'System.InvalidCastException' occurred in App_Code.iqmc5gba.dll'

Whilst this indicates that the error is occurring within the code held in the App_Code folder I'm actually none the wiser as there are 211 files that would appear to be contributing to the content of the dll as they are all named App_Code.iqmc5gba.nnn.cs where nnn is a number between 0 and 210

At the time ther error occurs the code is getting the data to display a record in a formview. The error only occurs on one record in the database that I can see at the moment.

Is anyone aware of any techniques that I can use to try and identify the line of code that is being executed at the point the invalidcastexception is encountered?

Upvotes: 0

Views: 10370

Answers (2)

jbolt
jbolt

Reputation: 688

I was having the same issues. Not sure if you have fixed your problem but Icarus is on the correct path. What I found was that in some cases you need to set the security question for an account to be created. I solved the problem by opening the ASP.Net Config under the Project Tab in VS2012. Click on the Provider Tab and Select a Provider for site management (Single in my case). Once that is active and you test the connection, when you go back to add a new user, there will be an option to enter a security question and answer. This seems to fix the problem.

Upvotes: 0

Icarus
Icarus

Reputation: 63956

This typically happens when reading data (using a datareader, for example) and certain column comes back null when you actually expect it to have at least some value. Consider this code:

 int age = (int)reader["age"]; //will throw ClassCastException if null

If for some reason the result set contains a null value on the age column, the value held by reader["age"] will not be an integer but rather a DBNull.Value which is a different type; hence the ClassCastException.

If you don't have access to the source code, your best bet is to make compare a good record with the bad one and make sure that corresponding columns on both records have values.

Upvotes: 1

Related Questions