PDA

View Full Version : ArrayList access


srhone
08-18-2004, 06:31 AM
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>
[b]
<form id="Form1" method="post" runat="server">
<asp:repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<td>[b]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>

bruce
08-18-2004, 11:47 AM
which line is erroring out?

[b]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>
[b]
<form id="Form1" method="post" runat="server">
<asp:repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<td>[b]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

srhone
08-19-2004, 06:35 AM
Sorry should have stated that: In the ShowPic method

foreach (PositionData POS in values)

Xenon
08-20-2004, 03:43 AM
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.

infinitedes
08-21-2004, 07:04 AM
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

KPayne
08-28-2004, 12:33 PM
[b]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 [quote]ArrayListvalues;</CODE> statement is executed with each request. But the subsequent [quote]if(!IsPostBack)</CODE> skips over the code that adds elements to the ArrayList. You are left with a valid variable containing NULL.

To solve this problem, you have a choice between caching the ArrayList between postbacks or re-loading the data from the OleDbDataReader each time. The decision of which procedure to use is based on an analysis of the size of the data, the amount of resources and time that it would take to re-query the data from the database as opposed to saving and retrieving the data from a cache, and the sensitivity of the data (security).

If the data set is small, go with the option of caching the data. You can choose to cache the data on the web server, on the client's PC in the form of a browser cookie, or on a dedicated State Server.

* If you are hosting the application with DASP, you are limited to the web server cache or a browser cookie.

To cache the data, change "values" from a local variable to a property of the class. In the get and set accessors, use the Session collection or the ViewState collection to cache and retrieve the ArrayList. Session saves the data on the web server and ViewState saves the data in a browser cookie (or in the _VIEWSTATE of the HTMLRequest object if the client has cookies disabled).

To reload the data with each request, move the OleDbDataReader code outside of the IsPostBack condition.

Check these topics in the VS.NET help for more info:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbconIntroductionToWebFormsStateManagement.htm

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconsessionstate.htm

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/mwsdk/html/mwconControllingSessionState.htm

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbconDecidingOnDataAccessStrategy.htm

Good luck!

Keith Payne
Technical Marketing Solutions