Hi,
When I call a method(present in external DLL) through reflection which having no parameters and void return ,it get called fine but in same DLL when I call a method which have no parameters but return type Datatable then I got the 'TargetInvocation' exception.
Can anybody help me ?

Recommended Answers

All 5 Replies

The TargetInvocationException tells you that the method you are calling has thrown an exception. Check the InnerException property of the exception to find out what the underlying cause might have been.

InnerException property of the exception gives
"Input array is longer than the number of columns in this table."

/***********************My Code*****************/
Datatable Mymethod()    //Method to call 
{
    Datatable dt_Temp=new Datatable();
       dt_Temp.Rows.Add(23,45);
    dt_Temp.Rows.Add(567,566);
     return dt_Temp;
}

methodSelected="MyMethod";

object ibaseObject = Activator.CreateInstance(type);
                    object result=new object(){};
        object[] arguments = new object[0];
  MethodInfo methodInfo=  type.GetMethod(methodSelected);
   result= methodInfo.Invoke(ibaseObject,arguments);

/************************End of code******************/

its the line dt_Temp.Rows.Add(23,45); that is throwing the exception. You are adding 2 values to the datatable but the datatable doesnt have 2 columns.
Try this:

Datatable dt_Temp = new DataTable();
            dt_Temp.Columns.Add(); //optionally use Columns.Add("name here") to name your columns
            dt_Temp.Columns.Add();
            dt_Temp.Rows.Add(23, 45);

Ryshad ............................
I don't have words to thanks u.
You are just awesome.
Thanks again for your so quick reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.