Using sender object in event handler

  • Thread starter Thread starter Ant
  • Start date Start date
A

Ant

Hi,
I'm tring to use the event handler in the statement completed event of a
command object to retrieve the value of the output parameter passed from the
SP.

When I try to do this, I get an 'Object referece not set to an instance of
an object'

What is going wrong here? Isn't sender the Command object? Shouldn't it have
a collection of the parameters & the value of the output param?

Below is what I'm trying to so in the event handler. I've broken up the
instantiation of the command obj to make it simpler.

void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// cast the sender obj to a command obj
SqlCommand commandResult = ((SqlCommand)sender);

// get the value of the output parm
string resultString =
commandResult.Parameters["@ResultString"].Value.ToString();

// display the value
textboxCompleted.Text = resultString;

}

many thanks for any help anyone can provide
Ant
 
Ant,

Normally I would put in a breakpoint at the first line of the method and
then hold the mouse over "Sender" to see what it is. It appears from your
error message that Sender is nothing, so I guess the command object never
initializes it.

Check in the "e" argument to see if the information you want is there.

Rick
 
Hi Rick,

Thanks for your time to answer this. Sorry but I didn't give the full
picture. Now I see it probably is very relevant. I'm doing this in an ASP.NET
app & I believe that when the button is clicked it does a postback which
would lose the state of the event params which might explain why there is no
'object reference'. So my issue might be, how to retain the state of the
events params when a postback occurs.

Thanks for your help
Ant

Rick said:
Ant,

Normally I would put in a breakpoint at the first line of the method and
then hold the mouse over "Sender" to see what it is. It appears from your
error message that Sender is nothing, so I guess the command object never
initializes it.

Check in the "e" argument to see if the information you want is there.

Rick

Ant said:
Hi,
I'm tring to use the event handler in the statement completed event of a
command object to retrieve the value of the output parameter passed from
the
SP.

When I try to do this, I get an 'Object referece not set to an instance of
an object'

What is going wrong here? Isn't sender the Command object? Shouldn't it
have
a collection of the parameters & the value of the output param?

Below is what I'm trying to so in the event handler. I've broken up the
instantiation of the command obj to make it simpler.

void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// cast the sender obj to a command obj
SqlCommand commandResult = ((SqlCommand)sender);

// get the value of the output parm
string resultString =
commandResult.Parameters["@ResultString"].Value.ToString();

// display the value
textboxCompleted.Text = resultString;

}

many thanks for any help anyone can provide
Ant
 
Back
Top