473,414 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How do I do data binding with an ArrayList? Gives error.

Hello,

I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that was
all confused with various other problems, which could be why no-one
answered it!! I've now narrowed the problem down, so am starting a new,
more specific, thread. Sorry it's a bit long, but it's reasonably simple
stuff.

I have a page where I'm showing product details, including any
non-priced variations available (say it comes in red, green and blue,
all at the same price), as well as any priced variations (say it comes
in small, medium and large, each at a different price).

I want to show the variation details in two repeaters, with all values
in editable controls. That way the user can edit as many as they like
without having to postback for each one.

I also have a set of controls where they can enter details for a new
variation. They click a link button and this creates an ArrayList
containing the variations from the existing repeater, plus the new one.
I then want to display this new list in the repeater. This is where I
get stuck ;-(

I have created a class for the variations as follows...

public class eCommVariation : IComparable {
public int avalueid; // the database ID
public string varName; // the displayed name (red, green, etc)
public bool showVar; // will this variation be shown?
public float price; // price (if a priced variation)
public bool specialOffer; // is it on special offer (if a priced
variation)
public float specialPrice; // special price (if a priced variation)

// method for IComparable so I can use the class for data binding
public int CompareTo(object objRhs) {
eCommVariation rhs = (eCommVariation)objRhs;
return String.Compare(this.varName, rhs.varName);
}
}

In the page itself, I pull the product details from a database when the
page first loads and display them. When the user clicks the "add new
variation" link button, the following code is called...

void lbtAddNpVar_Click(object o, EventArgs e) {
// add a new non-priced variation
// first set up the array of eCommVariation objects that will be used for databinding
ArrayList arrNpVars = new ArrayList();
// add the existing variations
for (int i=0; i<rptNpVars.Items.Count; i++) {
eCommVariation nPVar = new eCommVariation();
nPVar.avalueid = Convert.ToInt32(((Literal)rptNpVars.Items[i].FindControl("hidVarID")).Text);
nPVar.varName = ((TextBox)rptNpVars.Items[i].FindControl("txtNpVarName")).Text;
nPVar.showVar = ((CheckBox)rptNpVars.Items[i].FindControl("chkShowNpVar")).Checked;
// next three aren't relevant for non-priced variations, set them to dummy values
nPVar.price = 0;
nPVar.specialOffer = false;
nPVar.specialPrice = 0;
arrNpVars.Add(nPVar);
}
// now add the new value
eCommVariation newNpVar = new eCommVariation();
newNpVar.avalueid = 0; // shows it's a new value
newNpVar.varName = txtNewNpVar.Text;
newNpVar.showVar = chkShowNewNpVar.Checked;
// set the next three to anything as before
newNpVar.price = 0;
newNpVar.specialOffer = false;
newNpVar.specialPrice = 0;
arrNpVars.Add(newNpVar);
// so now we have the array of eCommVariation objects set up, sort it and bind it to the repeater
arrNpVars.Sort();
rptNpVars.DataSource = arrNpVars;
rptNpVars.DataBind();
}

(this code is for the non-priced variations, so the three class members
relevant to priced ones are just set to dummy values)

The problem is when the DataBind() method is called. The ItemTemplate of
the repeater looks like this (HTML simplified)...

<ItemTemplate>
<asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/>
<br><asp:TextBox ID="txtNpVarName" Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20" MaxLength="20" cssClass="box"
RunAt="server" />
<br><span style="color:#ffffff"><asp:Literal ID="hidVarID" Text='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="server" /></span>
<br><asp:CheckBox ID="chkShowNpVar" Text="" Checked='<%#TrueIfY((string)DataBinder.Eval(Contai ner.DataItem, "show_avalue"))%>'
RunAt="server"/>
</ItemTemplate>

When it tries to do the data binding, it gives an error
"DataBinder.Eval: 'PixataWebUtils.eCommVariation' does not contain a
property with the name avalue" on the line...

<br><asp:TextBox ID="txtNpVarName"
Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
MaxLength="20" cssClass="box" RunAt="server" />

Now I know the class *does* contain a property (well, actually a public
variable, but I think that amounts to the same thing doesn't it?) with
the name "avalue". If I swap the order of the controls in the
ItemTemplate, then it gives the error on whichever comes first, so it's
not actually that the property doesn't exist, it's that it doesn't like
*any* of them.

What am I doing wrong here? It seems such a simple, basic thing that I'm
stuck. I know you can do data binding with an ArrayList, but I must be
missing some basic point here.

Any suggestions welcome. TIA.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
4 1957
Alan:
No, a public field doesn't amount to the same thing as a public property.
That's your problem, plain and simple. Besides, you shouldn't have public
fields...you should ALWAYS use properties.

BTW, I appreciate the detail of the post, but if no one answered your other
one, it might just be because of how long they are. I had to read
everything just to get to the bottom and get to the real root of the
problem. It's kinda a catch 22, sometimes we need more details and have to
ask for it, sometimes we get too much :) One thing that might have helped
is a simplified example. Your class could have contained just 2
fields...and your btn click could have been simplified just to create a
couple new instances of said class, added to an array list and binded. Just
some thoughts for the future :)

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:1L**************@nospamthankyou.spam...
Hello,

I'm trying to use an ArrayList to do data binding, but am getting an
error I don't understand. I posted this in another thread, but that was
all confused with various other problems, which could be why no-one
answered it!! I've now narrowed the problem down, so am starting a new,
more specific, thread. Sorry it's a bit long, but it's reasonably simple
stuff.

I have a page where I'm showing product details, including any
non-priced variations available (say it comes in red, green and blue,
all at the same price), as well as any priced variations (say it comes
in small, medium and large, each at a different price).

I want to show the variation details in two repeaters, with all values
in editable controls. That way the user can edit as many as they like
without having to postback for each one.

I also have a set of controls where they can enter details for a new
variation. They click a link button and this creates an ArrayList
containing the variations from the existing repeater, plus the new one.
I then want to display this new list in the repeater. This is where I
get stuck ;-(

I have created a class for the variations as follows...

public class eCommVariation : IComparable {
public int avalueid; // the database ID
public string varName; // the displayed name (red, green, etc)
public bool showVar; // will this variation be shown?
public float price; // price (if a priced variation)
public bool specialOffer; // is it on special offer (if a priced
variation)
public float specialPrice; // special price (if a priced variation)

// method for IComparable so I can use the class for data binding
public int CompareTo(object objRhs) {
eCommVariation rhs = (eCommVariation)objRhs;
return String.Compare(this.varName, rhs.varName);
}
}

In the page itself, I pull the product details from a database when the
page first loads and display them. When the user clicks the "add new
variation" link button, the following code is called...

void lbtAddNpVar_Click(object o, EventArgs e) {
// add a new non-priced variation
// first set up the array of eCommVariation objects that will be used for
databinding
ArrayList arrNpVars = new ArrayList();
// add the existing variations
for (int i=0; i<rptNpVars.Items.Count; i++) {
eCommVariation nPVar = new eCommVariation();
nPVar.avalueid =
Convert.ToInt32(((Literal)rptNpVars.Items[i].FindControl("hidVarID")).Text);
nPVar.varName =
((TextBox)rptNpVars.Items[i].FindControl("txtNpVarName")).Text;
nPVar.showVar =
((CheckBox)rptNpVars.Items[i].FindControl("chkShowNpVar")).Checked;
// next three aren't relevant for non-priced variations, set them to
dummy values
nPVar.price = 0;
nPVar.specialOffer = false;
nPVar.specialPrice = 0;
arrNpVars.Add(nPVar);
}
// now add the new value
eCommVariation newNpVar = new eCommVariation();
newNpVar.avalueid = 0; // shows it's a new value
newNpVar.varName = txtNewNpVar.Text;
newNpVar.showVar = chkShowNewNpVar.Checked;
// set the next three to anything as before
newNpVar.price = 0;
newNpVar.specialOffer = false;
newNpVar.specialPrice = 0;
arrNpVars.Add(newNpVar);
// so now we have the array of eCommVariation objects set up, sort it and
bind it to the repeater
arrNpVars.Sort();
rptNpVars.DataSource = arrNpVars;
rptNpVars.DataBind();
}

(this code is for the non-priced variations, so the three class members
relevant to priced ones are just set to dummy values)

The problem is when the DataBind() method is called. The ItemTemplate of
the repeater looks like this (HTML simplified)...

<ItemTemplate>
<asp:CheckBox ID="chkDeleteNpVar" Text="" Checked="False" RunAt="server"/>
<br><asp:TextBox ID="txtNpVarName"
Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
MaxLength="20" cssClass="box"
RunAt="server" />
<br><span style="color:#ffffff"><asp:Literal ID="hidVarID"
Text='<%#DataBinder.Eval(Container.DataItem, "avalueid")%>' RunAt="server"
/></span>
<br><asp:CheckBox ID="chkShowNpVar" Text=""
Checked='<%#TrueIfY((string)DataBinder.Eval(Contai ner.DataItem,
"show_avalue"))%>'
RunAt="server"/>
</ItemTemplate>

When it tries to do the data binding, it gives an error
"DataBinder.Eval: 'PixataWebUtils.eCommVariation' does not contain a
property with the name avalue" on the line...

<br><asp:TextBox ID="txtNpVarName"
Text='<%#DataBinder.Eval(Container.DataItem, "avalue")%>' Columns="20"
MaxLength="20" cssClass="box" RunAt="server" />

Now I know the class *does* contain a property (well, actually a public
variable, but I think that amounts to the same thing doesn't it?) with
the name "avalue". If I swap the order of the controls in the
ItemTemplate, then it gives the error on whichever comes first, so it's
not actually that the property doesn't exist, it's that it doesn't like
*any* of them.

What am I doing wrong here? It seems such a simple, basic thing that I'm
stuck. I know you can do data binding with an ArrayList, but I must be
missing some basic point here.

Any suggestions welcome. TIA.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
>Alan:
No, a public field doesn't amount to the same thing as a public property.
That's your problem, plain and simple.
Duh, I knew it was going to be something simple. Thanks very much.
Besides, you shouldn't have public
fields...you should ALWAYS use properties.
I know, I was just being lazy ;-)

This class was for my personal use (being the only developer on this
site) and only for this page, so I figured (wrongly) I could take a
short-cut. I guess I'll know better next time eh?

Just out of interest, what is the difference between a public field and
a property? They look the same to the human eye, why does the framework
see them differently? Yes I am new at this, and yes I am trying to learn
to understand and do it right!!
BTW, I appreciate the detail of the post, but if no one answered your other
one, it might just be because of how long they are. I had to read
everything just to get to the bottom and get to the real root of the
problem.
Yeah, I know. I had the same thought when I posted it, but I couldn't
see how to simplify it without writing a new one (which I probably
should have done!!). I also get put off by long posts ;-)

Thanks very much for the help,

Alan
It's kinda a catch 22, sometimes we need more details and have to
ask for it, sometimes we get too much :) One thing that might have helped
is a simplified example. Your class could have contained just 2
fields...and your btn click could have been simplified just to create a
couple new instances of said class, added to an array list and binded. Just
some thoughts for the future :)

Karl


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3
Fields vs Properties, it's actually the opposite. Properties are a lot more
like methods than fields. Actually, they are special methods. I'm not sure
why databinding only works against properties...I know you can reflect
against public fields (assuming databinding works by reflection)...I figure
it's just something they did out of convention - you aren't ever supposed to
have public fields anyways, so why would they support binding against them?
You want properties because it provides some encapsulation...if something
changes and ur using fields, you'll break code that's consuming yoru class.
Using properties, there's a good chance you'll be able to encapsulate the
change and calling code will be ignorant of the fact..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:re**************@nospamthankyou.spam...
Alan:
No, a public field doesn't amount to the same thing as a public property.
That's your problem, plain and simple.


Duh, I knew it was going to be something simple. Thanks very much.
Besides, you shouldn't have public
fields...you should ALWAYS use properties.


I know, I was just being lazy ;-)

This class was for my personal use (being the only developer on this site)
and only for this page, so I figured (wrongly) I could take a short-cut. I
guess I'll know better next time eh?

Just out of interest, what is the difference between a public field and a
property? They look the same to the human eye, why does the framework see
them differently? Yes I am new at this, and yes I am trying to learn to
understand and do it right!!
BTW, I appreciate the detail of the post, but if no one answered your
other
one, it might just be because of how long they are. I had to read
everything just to get to the bottom and get to the real root of the
problem.


Yeah, I know. I had the same thought when I posted it, but I couldn't see
how to simplify it without writing a new one (which I probably should have
done!!). I also get put off by long posts ;-)

Thanks very much for the help,

Alan
It's kinda a catch 22, sometimes we need more details and have to
ask for it, sometimes we get too much :) One thing that might have
helped
is a simplified example. Your class could have contained just 2
fields...and your btn click could have been simplified just to create a
couple new instances of said class, added to an array list and binded.
Just
some thoughts for the future :)

Karl


--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #4
Karl,

Thanks for the clarification. Reading your words made a lot of sense. I
did some Java a few years back and got quite into OOP, but then I spent
several years since doing only VB, so the principles got weak.

I guess I just need to learn not be so lazy!!

Ta ra
a
Fields vs Properties, it's actually the opposite. Properties are a lot more
like methods than fields. Actually, they are special methods. I'm not sure
why databinding only works against properties...I know you can reflect
against public fields (assuming databinding works by reflection)...I figure
it's just something they did out of convention - you aren't ever supposed to
have public fields anyways, so why would they support binding against them?
You want properties because it provides some encapsulation...if something
changes and ur using fields, you'll break code that's consuming yoru class.
Using properties, there's a good chance you'll be able to encapsulate the
change and calling code will be ignorant of the fact..

Karl


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kk | last post by:
Have 2 problems, any help is appreciated. Tab with Grids -------------- BL - fetching data from DB ( 5 secs - 10 rows) Grid Laod - 20 secs Grid Paint on tab change - 20 secs Problem: The...
0
by: Duncan | last post by:
It is on WINCE. But should not matter. I am binding a datagrid to a ArrayList using the datasource property of the datagrid. ArrayList will contain data structures. The data structure contains all...
2
by: John Sun | last post by:
Hi, Dear gurus, I want to bind my collection to a datagrid. The field names in my object show up in a weird order, and I just wonder how I could reorder those columns . I find some code like...
2
by: Ivan Demkovitch | last post by:
Hi! I wonder if thats a bug or something: I have an object which has arrayList of child objects exposed to clients. This child object also exposed to clients. Class A { public ArrayList ...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
1
by: Dean Slindee | last post by:
Can someone explain how to remedy the two late binding errors below when Option Strict On: Public Function ControlDataRowArrayListPaint(ByRef frm As Form, _ ByRef ctl As Control, _ ByRef...
2
by: Dean Slindee | last post by:
Can someone explain how to remedy the two late binding errors below when Option Strict On: Public Function ControlDataRowArrayListPaint(ByRef frm As Form, _ ByRef ctl As Control, _ ByRef dr...
9
by: Miro | last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can. I have a variable defined as such: ( simple example ) Dim AVariableOfSorts(,) As Object = _ { _ {"Last", "String",...
3
by: kevinwolfe | last post by:
Hi all. I'd like any suggestions on how I can get my data set (not a DataSet) bound to a couple of controls on a form. Let me start by describing what my data looks like. Each entry correlates...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.