Class generates error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gorden blom

    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();
    }
    }
    }
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Class generates error

    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]
    - mvp@spam.guard. caspershouse.co m

    "gorden blom" <gordenblom@hot mail.com> wrote in message
    news:8c25c8c2.0 311100549.8d937 9a@posting.goog le.com...[color=blue]
    > 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();
    > }
    > }
    > }[/color]


    Comment

    • gorden blom

      #3
      Re: Class generates error

      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]" <mvp@spam.guard .caspershouse.c om> wrote in message news:<eSVHiI5pD HA.2940@TK2MSFT NGP09.phx.gbl>. ..[color=blue]
      > 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]
      > - mvp@spam.guard. caspershouse.co m
      >[/color]

      Comment

      Working...