473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.Tables(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
Nov 18 '05 #1
5 1233
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:
The following line of code works fine in my code-behind file for an aspx
page:

Dim HeadingRow As DataRow = dsGridReport.Tables(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

Nov 18 '05 #2
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.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
The following line of code works fine in my code-behind file for an aspx
page:

Dim HeadingRow As DataRow = dsGridReport.Tables(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

Nov 18 '05 #3
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 "dsGridReport." 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.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
The following line of code works fine in my code-behind file for an aspx
page:

Dim HeadingRow As DataRow = dsGridReport.Tables(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

Nov 18 '05 #4
Wow, a flame from a weenie. It's been years.
Gary :)

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
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 "dsGridReport." 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.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
The following line of code works fine in my code-behind file for an aspx
page:

Dim HeadingRow As DataRow = dsGridReport.Tables(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


Nov 18 '05 #5
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.com> wrote in message
news:uP**************@tk2msftngp13.phx.gbl...
Wow, a flame from a weenie. It's been years.
Gary :)

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:O5**************@tk2msftngp13.phx.gbl...
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 "dsGridReport." 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.com> wrote in message
news:OL**************@TK2MSFTNGP12.phx.gbl...
The following line of code works fine in my code-behind file for an aspx page:

Dim HeadingRow As DataRow = dsGridReport.Tables(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



Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: JohnSmith | last post by:
I suspect this is easy, but I have been stumped for a day trying to solve this.. I want to be able to have an unlimited number of aspx pages that all use the code in one class file. I want code...
6
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
4
by: bruce.1 | last post by:
It's my understanding that the next version of Visual Studio and ASP.NET will have the code-behind inline with the HTML, in the same file. It's also my understanding that Visual Studio will...
7
by: Shapper | last post by:
Hello, When creating an ASP.NET/VB web site I place my code in a code behind page, i.e., aspx.vb. Sometimes I have functions which are common to all aspx files. Is it possible in page1.aspx...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
3
by: needin4mation | last post by:
Hi, I was trying to update some 1.1 code with 2005. I ran into problems, so I thought I'd just use notepad to make a minor change to the .cs file. When I run the .aspx file (and refresh), nothing...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.