code in class versus aspx page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GaryB

    code in class versus aspx page

    The following line of code works fine in my code-behind file for an aspx
    page:

    Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )

    But when I transport that same code to a class and run it I get

    "Object reference not set to an instance of an object"

    Why?
    Gary


  • John M Deal

    #2
    Re: code in class versus aspx page

    When you move it to a class I'm guessing you mean on separate from the
    code behind class and that you are putting the line into a method of
    some sort. Given this scenario you could end up with this problem for a
    number of reasons:

    First if you don't pass an instance of the dataset (dsGridReport) to the
    class either as a property, a member lever variable, or a parameter of
    the method it wouldn't be able to be seen by the line of code. This is
    if the dataset is being created in the aspx page or its code behind.

    Second would be if the dataset didn't have a table in it. This could be
    caused if you were creating a data table and adding it to the table in
    the codebehind, but are no longer doing that. In this instance you need
    to make sure that you fully build the datatable and its column
    definitions prior to trying to add a row to it. This can happen either
    by filling the dataset from a datasource or by manually creating the
    datatable and defining its columns.

    These are the two ideas that spring to mind at this point. When you
    string together the calls to objects like this it is hard to pinpoint
    which one is causing the problem given limited information like this. To
    get a better idea of what the actual issue is I'd set a break point on
    the line and use the locals and or immediate/command window to find out
    which object really has the issue then resolve where it would be created
    and how that needs to be corrected for the line of code in question to
    succeed.

    Sorry I couldn't be more help.

    Have A Better One!

    John M Deal, MCP
    Necessity Software

    GaryB wrote:[color=blue]
    > The following line of code works fine in my code-behind file for an aspx
    > page:
    >
    > Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )
    >
    > But when I transport that same code to a class and run it I get
    >
    > "Object reference not set to an instance of an object"
    >
    > Why?
    > Gary
    >
    >[/color]

    Comment

    • Kevin Spencer

      #3
      Re: code in class versus aspx page

      An ASPX Page IS a class. So, what you're saying is, if you move a single
      line of code in one class that has no problems in that class where you have
      it, to another class (where in that class, you don't mention), it throws an
      exception. And you want to know why?

      ....

      ....

      ....

      Step away from the computer. Slowly.

      --
      ;-),
      Kevin Spencer
      ..Net Developer
      Microsoft MVP
      Neither a follower
      nor a lender be.

      "GaryB" <gb@nospam.co m> wrote in message
      news:OL6bytdzEH A.3120@TK2MSFTN GP12.phx.gbl...[color=blue]
      > The following line of code works fine in my code-behind file for an aspx
      > page:
      >
      > Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )
      >
      > But when I transport that same code to a class and run it I get
      >
      > "Object reference not set to an instance of an object"
      >
      > Why?
      > Gary
      >
      >[/color]


      Comment

      • Kevin Spencer

        #4
        Re: code in class versus aspx page

        Okay, I'm trying to calm down from my incredulity, so that I can give you a
        useful answer.

        That single line of code is ONLY going to work in a method of a class which
        has declared AND instantiated a DataSet named "dsGridRepo rt." In addition,
        that DataSet must contain at least ONE table. If either the DataSet named in
        that line of code doesn't exist, OR it has no tables in it, you will get the
        exception you describe, as you are referencing an object (either
        dsGridReport or its' table) that doesn't exist in that method or class.

        You wouldn't take a carbeurator out of your car, stick it in another car of
        another type, and expect it to work, would you?

        --
        HTH,
        Kevin Spencer
        ..Net Developer
        Microsoft MVP
        Neither a follower
        nor a lender be.

        "GaryB" <gb@nospam.co m> wrote in message
        news:OL6bytdzEH A.3120@TK2MSFTN GP12.phx.gbl...[color=blue]
        > The following line of code works fine in my code-behind file for an aspx
        > page:
        >
        > Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )
        >
        > But when I transport that same code to a class and run it I get
        >
        > "Object reference not set to an instance of an object"
        >
        > Why?
        > Gary
        >
        >[/color]


        Comment

        • GaryB

          #5
          Re: code in class versus aspx page

          Wow, a flame from a weenie. It's been years.
          Gary :)

          "Kevin Spencer" <kspencer@takem pis.com> wrote in message
          news:O57H1FkzEH A.2040@tk2msftn gp13.phx.gbl...[color=blue]
          > Okay, I'm trying to calm down from my incredulity, so that I can give you
          > a
          > useful answer.
          >
          > That single line of code is ONLY going to work in a method of a class
          > which
          > has declared AND instantiated a DataSet named "dsGridRepo rt." In addition,
          > that DataSet must contain at least ONE table. If either the DataSet named
          > in
          > that line of code doesn't exist, OR it has no tables in it, you will get
          > the
          > exception you describe, as you are referencing an object (either
          > dsGridReport or its' table) that doesn't exist in that method or class.
          >
          > You wouldn't take a carbeurator out of your car, stick it in another car
          > of
          > another type, and expect it to work, would you?
          >
          > --
          > HTH,
          > Kevin Spencer
          > .Net Developer
          > Microsoft MVP
          > Neither a follower
          > nor a lender be.
          >
          > "GaryB" <gb@nospam.co m> wrote in message
          > news:OL6bytdzEH A.3120@TK2MSFTN GP12.phx.gbl...[color=green]
          >> The following line of code works fine in my code-behind file for an aspx
          >> page:
          >>
          >> Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )
          >>
          >> But when I transport that same code to a class and run it I get
          >>
          >> "Object reference not set to an instance of an object"
          >>
          >> Why?
          >> Gary
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Kevin Spencer

            #6
            Re: code in class versus aspx page

            That wasn't a flame. It was an answer to your question. The last one you'll
            get from this Weenie.

            Luser. (THAT was a flame)

            --
            HTH,
            Kevin Spencer
            ..Net Developer
            Microsoft MVP
            Neither a follower
            nor a lender be.

            "GaryB" <gb@nospam.co m> wrote in message
            news:uP0x6Q#zEH A.3408@tk2msftn gp13.phx.gbl...[color=blue]
            > Wow, a flame from a weenie. It's been years.
            > Gary :)
            >
            > "Kevin Spencer" <kspencer@takem pis.com> wrote in message
            > news:O57H1FkzEH A.2040@tk2msftn gp13.phx.gbl...[color=green]
            > > Okay, I'm trying to calm down from my incredulity, so that I can give[/color][/color]
            you[color=blue][color=green]
            > > a
            > > useful answer.
            > >
            > > That single line of code is ONLY going to work in a method of a class
            > > which
            > > has declared AND instantiated a DataSet named "dsGridRepo rt." In[/color][/color]
            addition,[color=blue][color=green]
            > > that DataSet must contain at least ONE table. If either the DataSet[/color][/color]
            named[color=blue][color=green]
            > > in
            > > that line of code doesn't exist, OR it has no tables in it, you will get
            > > the
            > > exception you describe, as you are referencing an object (either
            > > dsGridReport or its' table) that doesn't exist in that method or class.
            > >
            > > You wouldn't take a carbeurator out of your car, stick it in another car
            > > of
            > > another type, and expect it to work, would you?
            > >
            > > --
            > > HTH,
            > > Kevin Spencer
            > > .Net Developer
            > > Microsoft MVP
            > > Neither a follower
            > > nor a lender be.
            > >
            > > "GaryB" <gb@nospam.co m> wrote in message
            > > news:OL6bytdzEH A.3120@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > >> The following line of code works fine in my code-behind file for an[/color][/color][/color]
            aspx[color=blue][color=green][color=darkred]
            > >> page:
            > >>
            > >> Dim HeadingRow As DataRow = dsGridReport.Ta bles(0).NewRow( )
            > >>
            > >> But when I transport that same code to a class and run it I get
            > >>
            > >> "Object reference not set to an instance of an object"
            > >>
            > >> Why?
            > >> Gary
            > >>
            > >>[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...