HELP! Simple delegation won't work!

  • Thread starter Thread starter Chris Leonard
  • Start date Start date
C

Chris Leonard

I am a C# newbie with experience in other languages who is writing a C# app
that talks to a SQL Server. I have the connection working, the queries are
working, error handling (try/catch/finally) is working, and life is (mostly)
good. However, I tried to use a delegate to display informational and
warning message (these are non-error messages not trapped by catch) and this
is *not* working. Here is what I believe to be the relevant part of my
code. I am calling a SQL Server stored procedure that I *know* raises a
message with severity level of 10 (informational), but doing a step-through
shows that my handler is not firing at all. What am I missing? The
following code is inside my form class (it's a thick-client app), if that
matters.

public delegate void OleDbInfoMessageEventHandler (Object sender,
OleDbInfoMessageEventArgs e);

public class DBMessageHandler
{
public static void MessageHandler (Object sender,
OleDbInfoMessageEventArgs e)
{
int i;
for (i = 0; i < e.Errors.Count; i++)
{
MessageBox.Show (e.Errors.ToString());
}
}
}

//DBMessageHandler dbmh = new DBMessageHandler();
OleDbInfoMessageEventHandler dbmh = new
OleDbInfoMessageEventHandler(DBMessageHandler.MessageHandler);
 
Hello Miha, and thanks for your reply. Though it took me a while to figure
it out, my problem was that I was trying to follow the samples at
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconeventsdelegates.asp,
but I really needed the simpler samples which I eventually found at
http://msdn.microsoft.com/library/d...ide/html/cpconworkingwithconnectionevents.asp.
I'm still not sure why my original attempts (from the first samples) failed,
but I think I was trying to write code as though I were publishing an event
instead of subscribing to it. In any event (no pun intended), your simple
questions got me thinking, and eventually I found the right stuff. Thanks
again for the reply.

Chris

Miha Markic said:
Hi Chris,

How are you invoking your delegate?

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Chris Leonard said:
I am a C# newbie with experience in other languages who is writing a C# app
that talks to a SQL Server. I have the connection working, the queries are
working, error handling (try/catch/finally) is working, and life is (mostly)
good. However, I tried to use a delegate to display informational and
warning message (these are non-error messages not trapped by catch) and this
is *not* working. Here is what I believe to be the relevant part of my
code. I am calling a SQL Server stored procedure that I *know* raises a
message with severity level of 10 (informational), but doing a step-through
shows that my handler is not firing at all. What am I missing? The
following code is inside my form class (it's a thick-client app), if that
matters.

public delegate void OleDbInfoMessageEventHandler (Object sender,
OleDbInfoMessageEventArgs e);

public class DBMessageHandler
{
public static void MessageHandler (Object sender,
OleDbInfoMessageEventArgs e)
{
int i;
for (i = 0; i < e.Errors.Count; i++)
{
MessageBox.Show (e.Errors.ToString());
}
}
}

//DBMessageHandler dbmh = new DBMessageHandler();
OleDbInfoMessageEventHandler dbmh = new
OleDbInfoMessageEventHandler(DBMessageHandler.MessageHandler);

 
Back
Top