473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can't create delegate for DataAdapater.RowUpdated event (reflection)

I am trying to use reflection to add an event handler for the RowUpdated
event of the OracleDataAdapter object (ODP.NET), but the same thing can be
said for SqlDataAdapter if you only use reflection. The code I supplied is
based on the SqlDataAdapter with reflection.

The error occurs when trying to create the delegate that will be passed in
to EventInfo.AddEventHandler. I get the following error:
An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method.
The binding problem is with the second parameter to the event handler. It
is of type SqlRowUpdatedEventArgs. Again, in reality, I am trying to use
ODP.NET via reflection. So, in this example, I am not referencing the
System.Data or System.Data.Client namespace directly.

Without referencing the namespace, how would one declare a delegate's method
that has a parameter type that exists in a namespace not being referenced
(does that make sense?)?

I have included some sample code to illustrate the problem. The first
CreateDelegate will succeed with the using System.Data* lines *not*
commented out because you can use the SqlRowUpdatedEventArgs parameter. If
you comment out the "using System.Data" and "using System.Data.SqlClient"
lines, how do you create the delegate?

Again, I am illustrating my problem here using Sql types, because not
everyone has ODP.NET installed.

Tim

using System;

using System.Reflection;

// TAKE THE FOLLOWING TWO LINES OUT TO DEPEND SOLELY ON REFLECTION

using System.Data;

using System.Data.SqlClient;

namespace ReflectionEventTest

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

CreateSqlDelegate();

}

static public void CreateSqlDelegate()

{

Module systemData = GetSystemDataModule();

Type t = systemData.GetType( "System.Data.SqlClient.SqlDataAdapter" );

object sqlDAObj = Activator.CreateInstance( t );

EventInfo ei = t.GetEvent( "RowUpdated" );

// This one works because the second parameter in the handler is of type
SqlRowUpdatedEventArgs

Delegate d1 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsProperSql" );

// These other three produce the following error:

//

// An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll

//

// Additional information: Error binding to target method.

// Causes error

Delegate d2 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsObject" );

// Causes error

Delegate d3 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsBaseClass" );

// Causes error

Delegate d4 = Delegate.CreateDelegate( ei.EventHandlerType, typeof(
Class1 ), "rowUpdated_ParamIsIntPtr" );

}

// THIS WON'T COMPILE WHEN YOU TAKE THE using System.Data* LINES OUT AT THE
TOP

static private void rowUpdated_ParamIsProperSql(object sender,
SqlRowUpdatedEventArgs e)

{

}

static private void rowUpdated_ParamIsObject(object sender, object
oraRowUpdEventArgs)

{

}

// SqlRowUpdatedEventArgs is derived from
System.Data.Common.RowUpdatedEventArgs

static private void rowUpdated_ParamIsBaseClass(object sender,
System.Data.Common.RowUpdatedEventArgs e)

{

}

static private void rowUpdated_ParamIsIntPtr(object sender, System.IntPtr e)

{

}

static public Module GetSystemDataModule()

{

Assembly assembly = null;

Module module = null;

string strongName = "System.Data, PublicKeyToken=b77a5c561934e089,
Culture=neutral, Version=1.0.5000.0";

// Load the assembly

assembly = Assembly.Load( strongName );

// Get the module

module = assembly.GetModule( "System.Data.dll" );

return module;

}

}

}
Jul 21 '05 #1
4 2148
Hi Tim,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you cannot create a delegate throw
reflection. If there is any misunderstanding, please feel free to let me
know.

Based on my research, SqlRowUpdatedEventArgs contains enumeration values
StatementType. As this is not supported by current .NET Framework with
CreateDelegate, we cannot use it.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #2
Kevin,

Thanks for the reply, and if you say it can't be done because of a member of
SqlRowUpdatedEventArgs, I'll accept that. But, in general, I am still
curious as to the resolution of my problem in generic terms. Let me present
my problem another way without Sql or ODP.NET data types.

Suppose I have object O with and event E that are defined in a module M that
I am referencing in my code via reflection. M also defines object A which
is an argument to E. At compile-time, how can I have written code that will
create a delegate that uses A which the compiler knows nothing about?

Is there a way to bind the event delegate to a method with generic arguments
(object?) which, once called by the event, the method uses reflection to
work with the generic arguments (object?) to access their
properties/methods?

I noticed when using the reference that defines SqlRowUpdatedEventHandler
that it has an overloaded constructor. When adding a RowUpdated event with
System.Data.Client properly referenced, the code looks something like this:

SqlDataAdapter sda = new SqlDataAdapter(.....);

sda.RowUpdated += new SqlRowUpdatedEventHandler(sda_RowUpdated );

private static void sda_RowUpdated(object sender, SqlRowUpdatedEventArgs e)

{

}
But if you put your cursor anywhere in between the parentheses of the
SqlRowUpdatedEventHandler constructor and hit Ctrl-Shift-Space to bring up
IntelliSense, you can walk through its two constructors. The first
constructor takes a single argument like the code above illustrates, but the
second constructor takes two arguments, "object object, System.IntPtr
method".

Could the second constructor somehow be used when using reflection to define
the delegate?

Tim
Jul 21 '05 #3
Hi Tim,

As far as I know, currently, we don't have a workaround. Since you can use
another overload of constructor to create the delegate the reference to the
argument is still a SqlRowUpdatedEventArgs object which contains a
enumeration member which is not supported. Based on my research, the only
way to overcome this is make the enumeration value to be an integer. Sorry
for the inconvenience.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #4
Kevin,

Thank you for the information.

Tim
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Torben | last post by:
For test purposes I attach an event to a control, say a TextBox TextChanged event. At another time the same event delegate is attached to some other control, maybe a listbox. Same event function...
6
by: Bill Davidson | last post by:
All: I'd like to create a delegate for a property (as opposed to a method). Can I do this? If so, please teach me the syntactic magic. For example, suppose I have a boolean ready-only...
4
by: johngilmer | last post by:
I posted this question a couple days ago on , but didn't get any answers, so I will try here. My issue: there is a link button with an event handler for a Click event. I want to create another...
4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: keith.thornhill | last post by:
hi all, lets say i have a usercontrol which implements a custom interface. like so: ------------------------------------------------- interface IMyInterface sub buttonClick() end interface
0
by: slemen | last post by:
My GridView is bound in VB code to a DataTable. The RowUpdating event fires when the row's Update linkbutton is clicked and the row contains the old, pre-edited values (as is expected.) ...
1
by: kret | last post by:
Hi, this is my first post so first of all I would like to say hello :) Now getting to my problem. In my job I have to create an ActiveX control in .NET 1.1 that can be lunched from IE....
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.