473,657 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class generates error

Hi,

I'm a student who's working on a school project about ASP.NET / C#.
I'm having some problems with my class. I want to create an ArrayList
filled with results from a DataReader. It gives me the following
error:

Exception Details: System.NullRefe renceException: Object reference not
set to an instance of an object.

Line 29: MyDataObject.Cr eateArray();

When I remove this sentence everything works fine. I'm a newbie to
ASP.NET / C# so I hope someone can help me.

Thanks in advance,

Gorden Blom (ti-mon3aan)

Here's the code..

codebehind:

using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Data.Sql Client;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

//custom classes
using Gorden.CustomCl ass;
namespace Test {

public class MyCodeBehind : Page {

protected System.Web.UI.W ebControls.Repe ater rptMenu;

public void Page_Load(objec t sender, System.EventArg s e) {

DataObject MyDataObject = new DataObject();

MyDataObject.Op en();
SqlDataReader MyReader = MyDataObject.sq lReader("SELECT *
FROM [Categorie]", rptMenu);
MyDataObject.Cr eateArray();
MyDataObject.Cl oseReader();
MyDataObject.Cl ose();
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e) {
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent() {
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}

class:

using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Data.Sql Client;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Gorden.CustomCl ass {

public class DataObject {

private SqlConnection sqlCon;
private SqlDataReader returnReader;
public ArrayList MyArrayList;
public DataTable schemaTable;

public DataObject() {
sqlCon = new
SqlConnection(C onfigurationSet tings.AppSettin gs.Get("connect ionstring"));
}

public void Open() {

this.sqlCon.Ope n();
}

public void Close() {

this.sqlCon.Clo se();
}

public SqlDataReader sqlReader(strin g strSql, Repeater rptRep)
{

SqlCommand command = new SqlCommand(strS ql, this.sqlCon);

returnReader = command.Execute Reader();

this.dataBind(r ptRep);

return returnReader;

}

public void dataBind(Repeat er RptRep) {

RptRep.DataSour ce = returnReader;

RptRep.DataBind ();

}

public void CreateArray() {

schemaTable = returnReader.Ge tSchemaTable();

string strData;
strData = null;

foreach (DataRow myRow in schemaTable.Row s) {
strData = myRow[0].ToString();
MyArrayList.Add (strData);
}
}

public void CloseReader() {

returnReader.Cl ose();
}
}
}
Nov 15 '05 #1
2 1146
Gorden,

I am not sure, but I think that because you have already bound to the
data, you have cleared the reader. Because of this, the schema information
is not available anymore and returns null.

Have you tried turning on the trace for the page? It should show you
the line where it occurs (in the CreateArray method) if you set the debug
mode to true in the WEB.CONFIG file and then turn on the trace for the page.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"gorden blom" <go********@hot mail.com> wrote in message
news:8c******** *************** **@posting.goog le.com...
Hi,

I'm a student who's working on a school project about ASP.NET / C#.
I'm having some problems with my class. I want to create an ArrayList
filled with results from a DataReader. It gives me the following
error:

Exception Details: System.NullRefe renceException: Object reference not
set to an instance of an object.

Line 29: MyDataObject.Cr eateArray();

When I remove this sentence everything works fine. I'm a newbie to
ASP.NET / C# so I hope someone can help me.

Thanks in advance,

Gorden Blom (ti-mon3aan)

Here's the code..

codebehind:

using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Data.Sql Client;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

//custom classes
using Gorden.CustomCl ass;
namespace Test {

public class MyCodeBehind : Page {

protected System.Web.UI.W ebControls.Repe ater rptMenu;

public void Page_Load(objec t sender, System.EventArg s e) {

DataObject MyDataObject = new DataObject();

MyDataObject.Op en();
SqlDataReader MyReader = MyDataObject.sq lReader("SELECT *
FROM [Categorie]", rptMenu);
MyDataObject.Cr eateArray();
MyDataObject.Cl oseReader();
MyDataObject.Cl ose();
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e) {
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent() {
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}

class:

using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Data.Sql Client;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace Gorden.CustomCl ass {

public class DataObject {

private SqlConnection sqlCon;
private SqlDataReader returnReader;
public ArrayList MyArrayList;
public DataTable schemaTable;

public DataObject() {
sqlCon = new
SqlConnection(C onfigurationSet tings.AppSettin gs.Get("connect ionstring"));
}

public void Open() {

this.sqlCon.Ope n();
}

public void Close() {

this.sqlCon.Clo se();
}

public SqlDataReader sqlReader(strin g strSql, Repeater rptRep)
{

SqlCommand command = new SqlCommand(strS ql, this.sqlCon);

returnReader = command.Execute Reader();

this.dataBind(r ptRep);

return returnReader;

}

public void dataBind(Repeat er RptRep) {

RptRep.DataSour ce = returnReader;

RptRep.DataBind ();

}

public void CreateArray() {

schemaTable = returnReader.Ge tSchemaTable();

string strData;
strData = null;

foreach (DataRow myRow in schemaTable.Row s) {
strData = myRow[0].ToString();
MyArrayList.Add (strData);
}
}

public void CloseReader() {

returnReader.Cl ose();
}
}
}

Nov 15 '05 #2
Nicholas,

I turned on the trace, I added this to the web.config file:

<system.web>
<compilation defaultLanguage ="C#" debug="true" />
<trace enabled="true" requestLimit="1 0" pageOutput="tru e"
traceMode="Sort ByTime"
localOnly="true "/>
</system.web>

When the page generates an error it doesn't give me a line number of
my class, it gives me the same line of the codebehind as before.

I removed the DataBind method, so the reader should still have all
it's information. When I tryed it again it gives me the same result as
before, could there be something else?

Hope you can help me.

Gorden Blom(ti-mon3aan)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:<eS******* *******@TK2MSFT NGP09.phx.gbl>. ..
Gorden,

I am not sure, but I think that because you have already bound to the
data, you have cleared the reader. Because of this, the schema information
is not available anymore and returns null.

Have you tried turning on the trace for the page? It should show you
the line where it occurs (in the CreateArray method) if you set the debug
mode to true in the WEB.CONFIG file and then turn on the trace for the page.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

Nov 15 '05 #3

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

Similar topics

0
3024
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms to an XML schema (xsd).
1
2248
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type 'System.Xml.XmlException' occured in system.xml.dll Addtional Information: System error |Break| |Continue| I clicked on the |Continue| and I got the following: "c:\Documents and Settings\Scott H. Chang\My Documents\Visual Studio...
3
1672
by: G. Purby | last post by:
What is the proper way of defining a class that inherits from a base class that has no constructor with 0 arguments? The following code generates compiler error CS1501, "No overload for method 'BaseClass' takes '0' arguments". class BaseClass { public BaseClass(double x) {
0
1162
by: jason | last post by:
i have classic ASP code that is calling a C# class library, which is wrapped for COM interop, and registered in the COM+ MMC. i have written 3 objects for the class library so far, and all three of them can be instantiated and used by the ASP classic application. however, i've just added a new object, and when i try to instantiate that object from the ASP application i get "Error '80070002'" which might mean "file not found," based on...
6
1312
by: Tappy Tibbons | last post by:
I do not know exactly how to explain what I am asking for, but here goes... Say I have a simple set of classes: ========= Public Class clsPerson Public FirstName As String Public LastName As String Public BillingAddress As clsAddress Public ShippingAddress As clsAddress End Class
0
1229
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a ComplexType A contains only one element, the tool don’t generate a class for the ComplexType A. It generates a class only for the element type (B) included in this ComplexType (if this type contains more than one element...). For types in WSDL schéma...
0
1985
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a ComplexType A contains only one element, the tool don’t generate a class for the ComplexType A. It generates a class only for the element type (B) included in this ComplexType (if this type contains more than one element...). For types in WSDL schéma...
8
3108
by: Tony Young | last post by:
Hi, The following code will have a compile error unless I remove "int *p" from class D (the error message is attached below). But I do need the *p. Someone suggested to change the initialization code to "S s = {D(d1, 3), D(d2, 4)};" assuming the constructor is provided. Is this the only way to resolve this problem? I prefer to modify class D and keep the initialization code intact. Would it be possible? Any help is much...
2
7211
by: Dinsdale | last post by:
We have created a object library that implements the INotifyPropertyChanged.PropertyChanged to bubble changes up to higher level classes. For instance, we have a person class that can have relationships with other persons. If there is a change in the relationship (i.e. status, type etc) then we want the PropertyChanged event to fire and notify the top level Person object of that change. The PropertyChanged event is implemented as follows:...
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.