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();
}
}
}
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();
}
}
}
Comment