SilverShotBee SilverShotBee - 5 months ago 52
Vb.net Question

VB.Net Exception has been thrown by the target of an invocation

I am using the following code in VB.Net (Winforms) to simply loop through a

DataGridView
and hide rows that are not needed.

Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged

For Each row In Incident_Persons_List.Rows
If Incident_Persons_List.Rows(CInt(row)).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(CInt(row)).Visible = True
Else
Incident_Persons_List.Rows(CInt(row)).Visible = False
End If
Next

End Sub


When the debugger gets to the first line of the
IF
statement, I get the following error:


An unhandled exception of type
'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

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


I've been trying everything I can think of to understand why this is. I've looked up the fault, but everyone seems to have completely different issues when this exception is thrown.

Is it something to do with how I am doing a compare?

UPDATE 1


  1. I have removed the
    For Each
    and replaced it with
    For i = 0 to Incident_Persons_list.Rows.Count

  2. I have removed the
    Cint
    instruction

  3. The
    Try/Catch
    as revealed that the actual exception being thrown is:




Row associated with the currency manager's position cannot be made
invisible.


UPDATE 2

Everything is now working correctly with the below code:

Private Sub Overview_Workstream_Sort_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Overview_Workstream_Sort.SelectedIndexChanged
Try
For i = 0 To Incident_Persons_List.Rows.Count - 1
If Incident_Persons_List.Rows(i).Cells(7).Value.ToString.Contains(Overview_Workstream_Sort.SelectedItem.ToString) Then
Debug.Print("User found in workstream")
Incident_Persons_List.Rows(i).Visible = True
Else


'Your code that will throw the Exception
Incident_Persons_List.CurrentCell = Nothing
Incident_Persons_List.Rows(i).Visible = False

End If
Next
Catch ex As TargetInvocationException
'We only catch this one, so you can catch other exception later on
'We get the inner exception because ex is not helpfull
Dim iEX = ex.InnerException
Debug.Print(iEX.Message)
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub


Thanks for the help!

Answer

TargetInvocationException :

The exception that is thrown by methods invoked through reflection

How to find out what's going on (because that exception is not really helpfull) ?

You must surroung the calling block with a Try/Catch structure, and then examine the InnerException caught :

Try
    'Your code that will throw the Exception
Catch ex As TargetInvocationException
    'We only catch this one, so you can catch other exception later on
    'We get the inner exception because ex is not helpfull
    Dim iEX = ex.InnerException
    'Now you can do some stuff to handle your exception
Catch ex As Exception
    'Here you catch other kinds of Exceptions that could occur in your code, if you want to...
End Try

And depending on the InnerException, you can now correct your code.