ArrayList access

Discussion in 'ASP.NET / ASP.NET Core' started by srhone, Aug 18, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Having an issue with an ArrayList on my ASP.Net page that is driving me nuts. I have a property with several field that I populate the ArrayList with, on the ASP.Net page I want to access those properties. I have tried the code in C# and it works fine but in ASP.NET I get an error "Object Referrence not set to an instance of an object". The code is below



    <%@ Import Namespace="System.Drawing"%>
    <%@ Page language="c#" Trace="false" Codebehind="pic_Flight.aspx.cs" AutoEventWireup="true" Inherits="Flight.Flight" %>
    <%@ Import Namespace="System.Data.OleDb"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Flying time</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <script runat="server">
    string pic_Path = "";
    string pic_DataType = "";
    ArrayList values;
    void Page_Load(object s , EventArgs e)
    {
    //Bitmap objMap;

    //objMap = CreateGraph();
    //objMap.Save(Response.OutputStream,ImageFormat.Gif);


    if (!IsPostBack)
    {
    OleDbDataReader oleReader = GetDataReader();


    values = new ArrayList();
    string buildImage = "";
    string buildThumb = "";
    string Desc = "";
    string strID = "";

    while(oleReader.Read())
    {
    buildImage = returnPath(oleReader.GetString(1),1);
    buildThumb = returnPath(oleReader.GetString(2),2);

    if (oleReader.IsDBNull(3))
    Desc = "";
    else
    Desc = oleReader.GetString(3);

    strID = clnString(oleReader.GetString(1));

    values.Add(new PositionData( buildImage, buildThumb, strID,Desc));
    pic_Path = buildImage;
    pic_DataType = Desc;
    }

    oleReader.Close();
    CloseConn();

    Repeater1.DataSource = values;
    Repeater1.DataBind();
    }
    }


    void ShowPic(object s, CommandEventArgs e)
    {
    pic_Path = e.CommandArgument.ToString();
    foreach (PositionData POS in values)
    {
    if (POS.Name == pic_Path)
    {
    if (Convert.IsDBNull(POS.Desc) == false)
    pic_DataType = POS.Desc;
    }
    }
    }

    public class PositionData
    {

    private string name;
    private string ticker;
    private string ID;
    private string desc;

    public PositionData(string name, string ticker,string id,string strDesc)
    {
    this.name = name;
    this.ticker = ticker;
    this.ID = id;
    this.desc = strDesc;
    }

    public string Name
    {
    get {return name;}
    }

    public string Ticker
    {
    get {return ticker;}
    }
    public string Id
    {
    get {return ID;}
    }
    public string Desc
    {
    get {return desc;}
    }
    }
    </script>
    </HEAD>

    <form id="Form1" method="post" runat="server">
    <asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <td>Picture</b></td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <asp:ImageButton ID="acb" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Ticker") %>' OnCommand="ShowPic" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Name") %>' Runat="server"/>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:repeater>
    <img src='<%=pic_Path%>' style="Z-INDEX: 101; LEFT: 143px; POSITION: absolute; TOP: 53px">
    <span id="Desc" style="Z-INDEX: 102; LEFT: 146px; POSITION: absolute; TOP: 25px">
    <%=pic_DataType%>
    </span>
    </form>
    </P>
    </body>
    </HTML>
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    which line is erroring out?

    quote:Originally posted by srhone

    Having an issue with an ArrayList on my ASP.Net page that is driving me nuts. I have a property with several field that I populate the ArrayList with, on the ASP.Net page I want to access those properties. I have tried the code in C# and it works fine but in ASP.NET I get an error "Object Referrence not set to an instance of an object". The code is below



    <%@ Import Namespace="System.Drawing"%>
    <%@ Page language="c#" Trace="false" Codebehind="pic_Flight.aspx.cs" AutoEventWireup="true" Inherits="Flight.Flight" %>
    <%@ Import Namespace="System.Data.OleDb"%>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Flying time</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <script runat="server">
    string pic_Path = "";
    string pic_DataType = "";
    ArrayList values;
    void Page_Load(object s , EventArgs e)
    {
    //Bitmap objMap;

    //objMap = CreateGraph();
    //objMap.Save(Response.OutputStream,ImageFormat.Gif);


    if (!IsPostBack)
    {
    OleDbDataReader oleReader = GetDataReader();


    values = new ArrayList();
    string buildImage = "";
    string buildThumb = "";
    string Desc = "";
    string strID = "";

    while(oleReader.Read())
    {
    buildImage = returnPath(oleReader.GetString(1),1);
    buildThumb = returnPath(oleReader.GetString(2),2);

    if (oleReader.IsDBNull(3))
    Desc = "";
    else
    Desc = oleReader.GetString(3);

    strID = clnString(oleReader.GetString(1));

    values.Add(new PositionData( buildImage, buildThumb, strID,Desc));
    pic_Path = buildImage;
    pic_DataType = Desc;
    }

    oleReader.Close();
    CloseConn();

    Repeater1.DataSource = values;
    Repeater1.DataBind();
    }
    }


    void ShowPic(object s, CommandEventArgs e)
    {
    pic_Path = e.CommandArgument.ToString();
    foreach (PositionData POS in values)
    {
    if (POS.Name == pic_Path)
    {
    if (Convert.IsDBNull(POS.Desc) == false)
    pic_DataType = POS.Desc;
    }
    }
    }

    public class PositionData
    {

    private string name;
    private string ticker;
    private string ID;
    private string desc;

    public PositionData(string name, string ticker,string id,string strDesc)
    {
    this.name = name;
    this.ticker = ticker;
    this.ID = id;
    this.desc = strDesc;
    }

    public string Name
    {
    get {return name;}
    }

    public string Ticker
    {
    get {return ticker;}
    }
    public string Id
    {
    get {return ID;}
    }
    public string Desc
    {
    get {return desc;}
    }
    }
    </script>
    </HEAD>

    <form id="Form1" method="post" runat="server">
    <asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <table border="1">
    <tr>
    <td>Picture</b></td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td>
    <asp:ImageButton ID="acb" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Ticker") %>' OnCommand="ShowPic" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Name") %>' Runat="server"/>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:repeater>
    <img src='<%=pic_Path%>' style="Z-INDEX: 101; LEFT: 143px; POSITION: absolute; TOP: 53px">
    <span id="Desc" style="Z-INDEX: 102; LEFT: 146px; POSITION: absolute; TOP: 25px">
    <%=pic_DataType%>
    </span>
    </form>
    </P>
    </body>
    </HTML>

    </blockquote id="quote"></font id="quote">

    B.

    DiscountASP.NET
    http://www.DiscountASP.NET
     
  3. Sorry should have stated that: In the ShowPic method

    foreach (PositionData POS in values)
     
  4. I think, it is because asp.net "doesn't support" public variables in the same way like in c#. Try to put the object in the session variable, or something. Because asp.net doesn't "know" for which user it is, and so it sets the object null on each user action.

    sry for my bad english.
     
  5. Search for "Object Reference not set to an instance of an object"
    on google to see what types of things usually cause this error.
    I remember getting it but I can't remember why. What do you mean
    it works in C# but not ASP.NET? Did you test it as a .cs extension?

    Owen
     
  6. quote:Originally posted by Xenon

    I think, it is because asp.net "doesn't support" public variables in the same way like in c#. Try to put the object in the session variable, or something. Because asp.net doesn't "know" for which user it is, and so it sets the object null on each user action.

    sry for my bad english.
    </blockquote id="quote"></font id="quote">

    Xenon is correct, although the explanation is not very clear because of his English.

    In asp.net, each time the page is posted back, the code executes from the top down - including the variable declarations. Nothing is inherently preserved from one postback to the next.

    In your code, the
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page