Events & postbacks

A

Ant

Hi,

I am using a sqlCommand object to run a SP. The SP returns an output
parameter that I retrieve the value of, & display.
I want to use the StatementCompleted event of the command obj. to handle
this output param through the sender object after the command completes.

When I try to access this sender object (cast to a command object) & then
reference the output parm, I get an error:
"Object reference not set to an object"
indicating that the command object has probably lost its reference in the
post back.

How can I handle an event over a Postback?

Many thanks for any help on this. I'm really stuck
Ant
 
M

Michael Nemtsev [MVP]

Hello Ant,

Could your show the short but working code demonstrating the problem?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


A> Hi,
A>
A> I am using a sqlCommand object to run a SP. The SP returns an output
A> parameter that I retrieve the value of, & display. I want to use the
A> StatementCompleted event of the command obj. to handle this output
A> param through the sender object after the command completes.
A>
A> When I try to access this sender object (cast to a command object) &
A> then
A> reference the output parm, I get an error:
A> "Object reference not set to an object"
A> indicating that the command object has probably lost its reference in
A> the
A> post back.
A> How can I handle an event over a Postback?
A>
A> Many thanks for any help on this. I'm really stuck Ant
A>
 
A

Ant

Hi Michael,
I haven't included the command object set up but I have given it an OP param
& tested it by not using a statement complete event & it returns a value fine
after ExecuteNonQuery. It's only when I try to retrieve it in the event
handler via the event object (as below) that I get the error.

Thanks very much for helping out here

Ant

try
{
connection.Open();

// execute the SP
commandBackup.ExecuteNonQuery();

}
catch (Exception ex)
{ // exception handling here }

finally
{ connection.Close(); }


void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// retrieving the command object fromn the sender param of the event
SqlCommand commandResult = ((SqlCommand)sender);

// !! GET ERROR HERE when trying to access the param value

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

textboxCompleted.Text = resultString;
}
 
B

bruce barker

if you want to use asynchronous events on a web page, the request has to
wait for the events to finish beofre t can send back the response.
otherwise when the event completes there is no request object.

to handle this asynchronous tasks where added to the page. the page will
wait (but free the thread) for the async tasks to complete before doing
prerender.

-- bruce (sqlwork.com)
Hi Michael,
I haven't included the command object set up but I have given it an OP param
& tested it by not using a statement complete event & it returns a value fine
after ExecuteNonQuery. It's only when I try to retrieve it in the event
handler via the event object (as below) that I get the error.

Thanks very much for helping out here

Ant

try
{
connection.Open();

// execute the SP
commandBackup.ExecuteNonQuery();

}
catch (Exception ex)
{ // exception handling here }

finally
{ connection.Close(); }


void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// retrieving the command object fromn the sender param of the event
SqlCommand commandResult = ((SqlCommand)sender);

// !! GET ERROR HERE when trying to access the param value

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

textboxCompleted.Text = resultString;
}




Michael Nemtsev said:
Hello Ant,

Could your show the short but working code demonstrating the problem?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


A> Hi,
A>
A> I am using a sqlCommand object to run a SP. The SP returns an output
A> parameter that I retrieve the value of, & display. I want to use the
A> StatementCompleted event of the command obj. to handle this output
A> param through the sender object after the command completes.
A>
A> When I try to access this sender object (cast to a command object) &
A> then
A> reference the output parm, I get an error:
A> "Object reference not set to an object"
A> indicating that the command object has probably lost its reference in
A> the
A> post back.
A> How can I handle an event over a Postback?
A>
A> Many thanks for any help on this. I'm really stuck Ant
A>
 
A

Ant

Hi Bruce,

Thanks very much for clarifying that for me. Is this something that is not
normally done in ASP.NET? (The use of ADO related events)

If it is, how could this be done

Many thanks

Ant

bruce barker said:
if you want to use asynchronous events on a web page, the request has to
wait for the events to finish beofre t can send back the response.
otherwise when the event completes there is no request object.

to handle this asynchronous tasks where added to the page. the page will
wait (but free the thread) for the async tasks to complete before doing
prerender.

-- bruce (sqlwork.com)
Hi Michael,
I haven't included the command object set up but I have given it an OP param
& tested it by not using a statement complete event & it returns a value fine
after ExecuteNonQuery. It's only when I try to retrieve it in the event
handler via the event object (as below) that I get the error.

Thanks very much for helping out here

Ant

try
{
connection.Open();

// execute the SP
commandBackup.ExecuteNonQuery();

}
catch (Exception ex)
{ // exception handling here }

finally
{ connection.Close(); }


void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// retrieving the command object fromn the sender param of the event
SqlCommand commandResult = ((SqlCommand)sender);

// !! GET ERROR HERE when trying to access the param value

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

textboxCompleted.Text = resultString;
}




Michael Nemtsev said:
Hello Ant,

Could your show the short but working code demonstrating the problem?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


A> Hi,
A>
A> I am using a sqlCommand object to run a SP. The SP returns an output
A> parameter that I retrieve the value of, & display. I want to use the
A> StatementCompleted event of the command obj. to handle this output
A> param through the sender object after the command completes.
A>
A> When I try to access this sender object (cast to a command object) &
A> then
A> reference the output parm, I get an error:
A> "Object reference not set to an object"
A> indicating that the command object has probably lost its reference in
A> the
A> post back.
A> How can I handle an event over a Postback?
A>
A> Many thanks for any help on this. I'm really stuck Ant
A>
 
M

Michael Nemtsev [MVP]

Hello Ant,

Keep your results in session or cache
There is no standard way to inform asp.net from the changes on server except
the polling server

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


A> Hi Bruce,
A>
A> Thanks very much for clarifying that for me. Is this something that
A> is not normally done in ASP.NET? (The use of ADO related events)
A>
A> If it is, how could this be done
A>
A> Many thanks
A>
A> Ant
A>
A> "bruce barker" wrote:
A>
if you want to use asynchronous events on a web page, the request has
to wait for the events to finish beofre t can send back the response.
otherwise when the event completes there is no request object.

to handle this asynchronous tasks where added to the page. the page
will wait (but free the thread) for the async tasks to complete
before doing prerender.

-- bruce (sqlwork.com)
Hi Michael,
I haven't included the command object set up but I have given it an
OP param
& tested it by not using a statement complete event & it returns a
value fine
after ExecuteNonQuery. It's only when I try to retrieve it in the
event
handler via the event object (as below) that I get the error.
Thanks very much for helping out here

Ant

try
{
connection.Open();
// execute the SP
commandBackup.ExecuteNonQuery();
}
catch (Exception ex)
{ // exception handling here }
finally
{ connection.Close(); }
void commandBackup_StatementCompleted(object sender,
StatementCompletedEventArgs e)
{
// retrieving the command object fromn the sender param of the event
SqlCommand commandResult = ((SqlCommand)sender);
// !! GET ERROR HERE when trying to access the param value

// retrieve the value of the output parm
string resultString =
commandResult.Parameters["@ResultString"].Value.ToString();
textboxCompleted.Text = resultString;
}
:

Hello Ant,

Could your show the short but working code demonstrating the
problem?

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:
http://spaces.live.com/laflour
"The greatest danger for most of us is not that our aim is too high
and we miss it, but that it is too low and we reach it" (c)
Michelangelo

A> Hi,
A>
A> I am using a sqlCommand object to run a SP. The SP returns an
output
A> parameter that I retrieve the value of, & display. I want to use
the
A> StatementCompleted event of the command obj. to handle this
output
A> param through the sender object after the command completes.
A>
A> When I try to access this sender object (cast to a command
object) &
A> then
A> reference the output parm, I get an error:
A> "Object reference not set to an object"
A> indicating that the command object has probably lost its
reference in
A> the
A> post back.
A> How can I handle an event over a Postback?
A>
A> Many thanks for any help on this. I'm really stuck Ant
A>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top