Class generates error

G

gorden blom

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.Get("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();
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

gorden blom said:
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.Get("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();
}
}
}
 
G

gorden blom

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top