Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Exception has been thrown by the target of an invocation

On Thursday, November 3rd 2005 at 03:39 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.7 with 15 votes)
Advertisement
More C# Resources
You may receive this exception when you use the Invoke() method (of a form perhaps). The cause of this exception is another exception which occured inside the method being invoked.
Since you don't know what actually went wrong inside the method that was being invoked, we will need to get the inner exception of the method.
Suppose we have the following line which invokes a method:

MyForm.Invoke(MyForm.MyDelegate, new Object[] { someParameter });

To catch the exception, enclose the line within a try/catch block and get the InnerException:

try
{
   MyForm.Invoke(MyForm.MyDelegate, new Object[] { someParameter });
}
catch (Exception ex)
{
   MessageBox.Show(ex.InnerException.Message);
}

This should show you the actual exception message inside a message box.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by The Pope on Tuesday, April 25th 2006 at 10:12 AM

Hello World. How are you all. this is the pope.

by Zubair Ahmed on Tuesday, May 2nd 2006 at 05:45 AM

HI Andrei

Nice finding ... Helped me.

Thanks

by d on Wednesday, June 14th 2006 at 08:43 AM

by A thankful VB.Net Pr on Monday, July 31st 2006 at 08:26 PM

Thanks for the help... This helped me track down the actual problem.

Thanks.

by Shawn on Thursday, September 7th 2006 at 04:22 PM

helped me too, thanks!

by nitin on Tuesday, October 31st 2006 at 04:31 AM

very useful, i did find the solution for my problem.
Thnx a lot

by nooch on Thursday, November 23rd 2006 at 09:07 AM

...and if this error pops up when you're not calling the invoke method? :S

by nooch on Thursday, November 23rd 2006 at 09:09 AM

in my app this error occurs when i try and load a file into a pic box when the file is in use, any ideas?

by Balasubramanian on Tuesday, February 27th 2007 at 04:50 AM

THANK YOU so much....it helped me find the problem

by Vishwanath Patel on Wednesday, April 25th 2007 at 03:37 AM

It helped me a lot...

THANKS A LOT....

by Syrinx on Thursday, May 3rd 2007 at 08:08 AM

But in this case it is better, when you specify the correct Exception, because it\'s a very special Exception and it only occurs, if you do an Invoke of something.

It is an TargetInvocationException.
You can also catch the normal Exception in a separate block.

by Eirin on Wednesday, May 16th 2007 at 04:26 AM

Thanks! This helped me surfacing the real exception.

by Suji on Saturday, June 30th 2007 at 02:58 AM

Gud timely helped

by carl on Wednesday, July 4th 2007 at 11:19 AM

Thanks for you help.

Blessing from Guatemala!

by Arun on Saturday, July 7th 2007 at 09:59 AM

Yes, great stuff! Thanks a lot!!!

by jophez on Thursday, July 26th 2007 at 12:18 AM

tnx for the info! it really helped!!!!

by as on Wednesday, September 12th 2007 at 02:44 PM

dadsf dfd asded fdad

by Steve Waugh on Wednesday, September 19th 2007 at 02:42 PM

When i hit the insert button for an online form, thats when this error comes up...plz help

by Andrei Pociu on Thursday, September 20th 2007 at 10:53 AM

Steve, there can several reasons for which you are getting this exception. In fact, the message of this exception only tell's you another error occured at a different place. You'll need to use the code above to find out what the error is.

by Steve Waugh on Wednesday, October 3rd 2007 at 11:21 AM

Do I have to replace someParameter with something else. some other piece of code?

by Andrei Pociu on Wednesday, October 3rd 2007 at 03:03 PM

Steve - only if the method invoked has parameters being passed to it. But that's only a sample line of code anyway, the point of the FAQ answer is that you should wrap the piece of code that's causing the exception into a try/catch block, and use something such as a message box to show the inner exception.

by Steve Waugh on Wednesday, October 3rd 2007 at 04:52 PM

I'm a newbie in it. I'm just trying to maintain a site for my work using C# and Visual Web Developer Express. There's an online form, and for only one specific tab, ex: where you write your email address, that is where it produces the error.

by Andrei Pociu on Wednesday, October 3rd 2007 at 10:42 PM

In the error page look at the line where the error shows up. Open the CS file and wrap that line in try/catch blocks as in the example above. Since we're talking about a website here, you'll want to use Console.WriteLine() instead of MessageBox.Show(). Then go back to the page and you should see what the actual cause of the error is.

There are other ways to do it, but this one's the easiest.

by sandeep on Monday, November 5th 2007 at 04:55 AM

i had not used any invoke in my page load event still this error comes out any solution

Exception has been thrown by the target of an invocation.
5
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.GridView.DataBind()
at System.Web.UI.WebControls.BaseDataBoundControl.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.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

by David Morezz on Friday, November 23rd 2007 at 03:42 PM

Thanks sir

by Lily on Thursday, January 24th 2008 at 11:09 PM

Incredibly useful. Thank you muchly.

by eric on Thursday, February 14th 2008 at 09:46 PM

very useful information, many thanks for your sharing

by sarat on Monday, February 25th 2008 at 07:07 AM

Thank a lot...very useful

by Kalmi on Sunday, March 16th 2008 at 02:49 PM

It can also happen if you do InitializeComponent twice in a WPF app.

by Jeeva on Friday, September 12th 2008 at 01:03 AM

Thanks a lot:)

by gregor on Thursday, November 20th 2008 at 03:13 PM

not helpful for me

by 7Turturi on Tuesday, February 10th 2009 at 05:51 AM

True... Did help get to the core of the problem. nice one, keep them up!

by ajay on Tuesday, March 17th 2009 at 06:32 AM

why comming this Exception has been thrown by the target of an invocation in asp.net 1.1 and using win2003 O.S. when Ckick Export of Crystal report. ????

by Nate on Friday, April 24th 2009 at 03:34 PM

Does anyone know if there is a way to make VisualStudio take you to the line of the InnerException instead of making you view the InnerException and go there yourself??

by Tomas on Tuesday, September 15th 2009 at 05:42 PM

thanks, that helped!

by Tarun on Monday, October 5th 2009 at 03:04 AM

Hi Really it's help full to me.

It's nice and great post.

by Jmartini on Wednesday, October 7th 2009 at 08:12 PM

thanks for the help. what would i do without google and people who share...

by faezeh on Saturday, October 31st 2009 at 02:30 AM

thanks,help me alotttttttttttt!!!

by abcd on Wednesday, March 24th 2010 at 09:48 AM

Thanx,it helped me to track down real problem.

by The Gremlin on Monday, May 10th 2010 at 01:31 AM

Well this is nothing short of crap. Since I have Sharepoint in the middle I cant get to the actual exception. What would be the point of creating an exception whose only purpose appears to be masking another exception (so you can't diagnose it) is beyond me.

by Soniya on Monday, November 1st 2010 at 04:41 AM

Thanks a ton!!...really helped me a lot!!

by Kiran on Wednesday, November 3rd 2010 at 04:44 PM

thank u...

by Mohammad Nasir on Monday, March 21st 2011 at 11:53 AM

I would like to add something, i.e.

But, when using try catch to handle the exception, might be you are writting the exception or some sort of message on the response with <System.Web.HttpContext.Current.Response.Write> and hence your invked method says <Exception has been thrown by the target of an invocation>.

To avoid this exception, throw your exception and hadle it instead of writing any thing on response and then return a proper value [even NULL] in your method that you're invoking.

You can use the finally block to return the some default value in case if exception occures.

by mhiel cruz on Wednesday, September 28th 2011 at 09:07 AM

hi,need help.i download talk n talk,it successfully download to my pc,but when i try to open,it will appear Exeption has been thrown by the target of invocation.what does this means?and how to fix it,so that i can use the talk n talk.please reply.thanks.

by mhiel cruz on Wednesday, September 28th 2011 at 09:08 AM

hi,need help.i download talk n talk,it successfully download to my pc,but when i try to open,it will appear Exeption has been thrown by the target of invocation.what does this means?and how to fix it,so that i can use the talk n talk.please reply.thanks.

by Ruben on Thursday, November 17th 2011 at 12:07 PM

Thanks alot, I've been stuck debugging this problem for days.

by low interest rate credit cards on Sunday, December 18th 2011 at 12:33 PM

i liked it too much . better if u provide a program and give comments on it .such as a bank account program. Why don't you post your own solution Pavlo..... it would have sure taken less time than writing all that.

by polas on Sunday, January 22nd 2012 at 04:08 PM

Can you explain how to use it or to put it on the form cuz it has a lot of errors :(

by George Potter on Monday, February 13th 2012 at 08:16 AM

This was incredibly helpful. Turns out my error came from a string being null in a completely different part of the program to the one where the exception occurred. Being able to find out the inner exception error saved me hours of debugging :)

by George Potter on Monday, February 13th 2012 at 08:16 AM

This was incredibly helpful. Turns out my error came from a string being null in a completely different part of the program to the one where the exception occurred. Being able to find out the inner exception error saved me hours of debugging :)

by I Bak on Monday, July 2nd 2012 at 06:28 AM

Hi, I am getting a similar message "Exception has been thrown by the target of an invocation
****INNER EXCEPTION****

I have been trying to go through some of the suggestions mentioned above but can't understand a thing- I know my way around a pc quite well, but can somebody provide me with a step by step approach in how to solve this. I am not a programmer so need an idiots guide.

Your help would be greatly appreciated.

Thanks

Ibak


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources