473,388 Members | 1,326 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,388 software developers and data experts.

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.NullReferenceException: Object reference not
set to an instance of an object.

Line 29: MyDataObject.CreateArray();

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.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

//custom classes
using Gorden.CustomClass;
namespace Test {

public class MyCodeBehind : Page {

protected System.Web.UI.WebControls.Repeater rptMenu;

public void Page_Load(object sender, System.EventArgs e) {

DataObject MyDataObject = new DataObject();

MyDataObject.Open();
SqlDataReader MyReader = MyDataObject.sqlReader("SELECT *
FROM [Categorie]", rptMenu);
MyDataObject.CreateArray();
MyDataObject.CloseReader();
MyDataObject.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent() {
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

class:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Gorden.CustomClass {

public class DataObject {

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

public DataObject() {
sqlCon = new
SqlConnection(ConfigurationSettings.AppSettings.Ge t("connectionstring"));
}

public void Open() {

this.sqlCon.Open();
}

public void Close() {

this.sqlCon.Close();
}

public SqlDataReader sqlReader(string strSql, Repeater rptRep)
{

SqlCommand command = new SqlCommand(strSql, this.sqlCon);

returnReader = command.ExecuteReader();

this.dataBind(rptRep);

return returnReader;

}

public void dataBind(Repeater RptRep) {

RptRep.DataSource = returnReader;

RptRep.DataBind();

}

public void CreateArray() {

schemaTable = returnReader.GetSchemaTable();

string strData;
strData = null;

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

public void CloseReader() {

returnReader.Close();
}
}
}
Nov 15 '05 #1
2 1140
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.com

"gorden blom" <go********@hotmail.com> wrote in message
news:8c*************************@posting.google.co m...
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.NullReferenceException: Object reference not
set to an instance of an object.

Line 29: MyDataObject.CreateArray();

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.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

//custom classes
using Gorden.CustomClass;
namespace Test {

public class MyCodeBehind : Page {

protected System.Web.UI.WebControls.Repeater rptMenu;

public void Page_Load(object sender, System.EventArgs e) {

DataObject MyDataObject = new DataObject();

MyDataObject.Open();
SqlDataReader MyReader = MyDataObject.sqlReader("SELECT *
FROM [Categorie]", rptMenu);
MyDataObject.CreateArray();
MyDataObject.CloseReader();
MyDataObject.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e) {
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent() {
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

class:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Gorden.CustomClass {

public class DataObject {

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

public DataObject() {
sqlCon = new
SqlConnection(ConfigurationSettings.AppSettings.Ge t("connectionstring"));
}

public void Open() {

this.sqlCon.Open();
}

public void Close() {

this.sqlCon.Close();
}

public SqlDataReader sqlReader(string strSql, Repeater rptRep)
{

SqlCommand command = new SqlCommand(strSql, this.sqlCon);

returnReader = command.ExecuteReader();

this.dataBind(rptRep);

return returnReader;

}

public void dataBind(Repeater RptRep) {

RptRep.DataSource = returnReader;

RptRep.DataBind();

}

public void CreateArray() {

schemaTable = returnReader.GetSchemaTable();

string strData;
strData = null;

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

public void CloseReader() {

returnReader.Close();
}
}
}

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="10" pageOutput="true"
traceMode="SortByTime"
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.com> wrote in message news:<eS**************@TK2MSFTNGP09.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.com

Nov 15 '05 #3

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

Similar topics

0
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...
1
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...
3
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...
0
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...
6
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...
0
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...
0
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...
8
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
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...

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.