Events & postbacks

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QW50?=

    Events & postbacks

    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 StatementComple ted 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

  • Michael Nemtsev [MVP]

    #2
    Re: Events & postbacks

    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


    AHi,
    A>
    AI am using a sqlCommand object to run a SP. The SP returns an output
    Aparameter that I retrieve the value of, & display. I want to use the
    AStatementCompl eted event of the command obj. to handle this output
    Aparam through the sender object after the command completes.
    A>
    AWhen I try to access this sender object (cast to a command object) &
    Athen
    Areference the output parm, I get an error:
    A"Object reference not set to an object"
    Aindicating that the command object has probably lost its reference in
    Athe
    Apost back.
    AHow can I handle an event over a Postback?
    A>
    AMany thanks for any help on this. I'm really stuck Ant
    A>


    Comment

    • =?Utf-8?B?QW50?=

      #3
      Re: Events & postbacks

      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.E xecuteNonQuery( );

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

      finally
      { connection.Clos e(); }


      void commandBackup_S tatementComplet ed(object sender,
      StatementComple tedEventArgs e)
      {
      // retrieving the command object fromn the sender param of the event
      SqlCommand commandResult = ((SqlCommand)se nder);

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

      // retrieve the value of the output parm
      string resultString =
      commandResult.P arameters["@ResultStr ing"].Value.ToString ();

      textboxComplete d.Text = resultString;
      }




      "Michael Nemtsev [MVP]" wrote:
      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
      >
      >
      AHi,
      A>
      AI am using a sqlCommand object to run a SP. The SP returns an output
      Aparameter that I retrieve the value of, & display. I want to use the
      AStatementCompl eted event of the command obj. to handle this output
      Aparam through the sender object after the command completes.
      A>
      AWhen I try to access this sender object (cast to a command object) &
      Athen
      Areference the output parm, I get an error:
      A"Object reference not set to an object"
      Aindicating that the command object has probably lost its reference in
      Athe
      Apost back.
      AHow can I handle an event over a Postback?
      A>
      AMany thanks for any help on this. I'm really stuck Ant
      A>
      >
      >
      >

      Comment

      • bruce barker

        #4
        Re: Events & postbacks

        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)

        Ant wrote:
        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.E xecuteNonQuery( );
        >
        }
        catch (Exception ex)
        { // exception handling here }
        >
        finally
        { connection.Clos e(); }
        >
        >
        void commandBackup_S tatementComplet ed(object sender,
        StatementComple tedEventArgs e)
        {
        // retrieving the command object fromn the sender param of the event
        SqlCommand commandResult = ((SqlCommand)se nder);
        >
        // !! GET ERROR HERE when trying to access the param value
        >
        // retrieve the value of the output parm
        string resultString =
        commandResult.P arameters["@ResultStr ing"].Value.ToString ();
        >
        textboxComplete d.Text = resultString;
        }
        >
        >
        >
        >
        "Michael Nemtsev [MVP]" wrote:
        >
        >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
        >>
        >>
        >AHi,
        >A>
        >AI am using a sqlCommand object to run a SP. The SP returns an output
        >Aparameter that I retrieve the value of, & display. I want to use the
        >AStatementComp leted event of the command obj. to handle this output
        >Aparam through the sender object after the command completes.
        >A>
        >AWhen I try to access this sender object (cast to a command object) &
        >Athen
        >Areference the output parm, I get an error:
        >A"Object reference not set to an object"
        >Aindicating that the command object has probably lost its reference in
        >Athe
        >Apost back.
        >AHow can I handle an event over a Postback?
        >A>
        >AMany thanks for any help on this. I'm really stuck Ant
        >A>
        >>
        >>
        >>

        Comment

        • =?Utf-8?B?QW50?=

          #5
          Re: Events & postbacks

          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" wrote:
          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)
          >
          Ant wrote:
          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.E xecuteNonQuery( );

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

          finally
          { connection.Clos e(); }


          void commandBackup_S tatementComplet ed(object sender,
          StatementComple tedEventArgs e)
          {
          // retrieving the command object fromn the sender param of the event
          SqlCommand commandResult = ((SqlCommand)se nder);

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

          // retrieve the value of the output parm
          string resultString =
          commandResult.P arameters["@ResultStr ing"].Value.ToString ();

          textboxComplete d.Text = resultString;
          }




          "Michael Nemtsev [MVP]" wrote:
          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
          >
          >
          AHi,
          A>
          AI am using a sqlCommand object to run a SP. The SP returns an output
          Aparameter that I retrieve the value of, & display. I want to use the
          AStatementCompl eted event of the command obj. to handle this output
          Aparam through the sender object after the command completes.
          A>
          AWhen I try to access this sender object (cast to a command object) &
          Athen
          Areference the output parm, I get an error:
          A"Object reference not set to an object"
          Aindicating that the command object has probably lost its reference in
          Athe
          Apost back.
          AHow can I handle an event over a Postback?
          A>
          AMany thanks for any help on this. I'm really stuck Ant
          A>
          >
          >
          >
          >

          Comment

          • Michael Nemtsev [MVP]

            #6
            Re: Events & postbacks

            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


            AHi Bruce,
            A>
            AThanks very much for clarifying that for me. Is this something that
            Ais not normally done in ASP.NET? (The use of ADO related events)
            A>
            AIf it is, how could this be done
            A>
            AMany thanks
            A>
            AAnt
            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)
            >>
            >Ant wrote:
            >>
            >>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.Op en();
            >>// execute the SP
            >>commandBackup .ExecuteNonQuer y();
            >>}
            >>catch (Exception ex)
            >>{ // exception handling here }
            >>finally
            >>{ connection.Clos e(); }
            >>void commandBackup_S tatementComplet ed(object sender,
            >>StatementComp letedEventArgs e)
            >>{
            >>// retrieving the command object fromn the sender param of the event
            >>SqlCommand commandResult = ((SqlCommand)se nder);
            >>// !! GET ERROR HERE when trying to access the param value
            >>>
            >>// retrieve the value of the output parm
            >>string resultString =
            >>commandResult .Parameters["@ResultStr ing"].Value.ToString ();
            >>textboxComple ted.Text = resultString;
            >>}
            >>"Michael Nemtsev [MVP]" wrote:
            >>>
            >>>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)
            >>>Michelange lo
            >>>>
            >>>AHi,
            >>>A>
            >>>AI am using a sqlCommand object to run a SP. The SP returns an
            >>>output
            >>>Aparameter that I retrieve the value of, & display. I want to use
            >>>the
            >>>AStatementCo mpleted event of the command obj. to handle this
            >>>output
            >>>Aparam through the sender object after the command completes.
            >>>A>
            >>>AWhen I try to access this sender object (cast to a command
            >>>object) &
            >>>Athen
            >>>Areference the output parm, I get an error:
            >>>A"Object reference not set to an object"
            >>>Aindicatin g that the command object has probably lost its
            >>>reference in
            >>>Athe
            >>>Apost back.
            >>>AHow can I handle an event over a Postback?
            >>>A>
            >>>AMany thanks for any help on this. I'm really stuck Ant
            >>>A>

            Comment

            Working...