473,322 Members | 1,719 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,322 software developers and data experts.

List Box Crash When Several Items Added

I have looked everywhere I can for a solution to this problem but
nothing so far has worked.

I have two list boxes in my form. The first one (selAvailable) is
populated from an access database. The second (selSelected) is
populated by selecting items in selAvailable and clicking the add
button. In some situations there can be hundreds or thousands of items
in the selAvailable list box. If a user selects a few hundred or
clicks the Add All button the selSelected list box will begin to
populate until the whole page freezes then crashes.

I tried making the list box selSelected invisible before added items
to it but that did not help.

I know it seems that no user would ever want to have a list box with
so many items but in the context of my webpage it is needed.

Below is the Add All code that adds all of the items from the
selAvailable list box to the selSelected list box. I greatly
appreciate anyones help, thank you very much in advance.

~Julia

function AllAdd()
{
var i
var count
var selectedText
var selectedValue
var selAvailableLength = frmSelect.selAvailable.length
var selSelectedLength = frmSelect.selSelected.length
var newoption
var count = selSelectedLength + 0;

for (i = 0; i < selAvailableLength; i++)
{
selectedText = frmSelect.selAvailable.options[i].text;
selectedValue = frmSelect.selAvailable.options[i].value;
newoption = new Option(selectedText, selectedValue, false, false);
frmSelect.selSelected.options[count] = newoption;
count = count + 1;
frmSelect.selAvailable.options[i] = null;
selAvailableLength = frmSelect.selAvailable.length;
i--;
}

}

Feb 23 '07 #1
8 1777
On Feb 23, 5:06 am, JuliaRBar...@gmail.com wrote:
I have looked everywhere I can for a solution to this problem but
nothing so far has worked.
mmm.. have you tried with different browsers/OS?
What browser are you currently using?

About the code..

i-- inside the for loop is pure evil..
anyway.. i suppose that your code works so you can try:

use "display" property to hide the destination combo object (that's
really different than using the visibility prop)
try to delay the insert for loop using a setTimeout call.

hope this helps.
Ciao.
Seba

Feb 23 '07 #2
On Feb 22, 8:06 pm, JuliaRBar...@gmail.com wrote:
I have looked everywhere I can for a solution to this problem but
nothing so far has worked.

I have two list boxes in my form. The first one (selAvailable) is
populated from an access database. The second (selSelected) is
populated by selecting items in selAvailable and clicking the add
button. In some situations there can be hundreds or thousands of items
in the selAvailable list box. If a user selects a few hundred or
clicks the Add All button the selSelected list box will begin to
populate until the whole page freezes then crashes.

I tried making the list box selSelected invisible before added items
to it but that did not help.

I know it seems that no user would ever want to have a list box with
so many items but in the context of my webpage it is needed.

Below is the Add All code that adds all of the items from the
selAvailable list box to the selSelected list box. I greatly
appreciate anyones help, thank you very much in advance.

~Julia

function AllAdd()
{
var i
var count
var selectedText
var selectedValue
var selAvailableLength = frmSelect.selAvailable.length
var selSelectedLength = frmSelect.selSelected.length
var newoption

var count = selSelectedLength + 0;

for (i = 0; i < selAvailableLength; i++)
{
selectedText = frmSelect.selAvailable.options[i].text;
selectedValue = frmSelect.selAvailable.options[i].value;
newoption = new Option(selectedText, selectedValue, false, false);
frmSelect.selSelected.options[count] = newoption;
count = count + 1;
frmSelect.selAvailable.options[i] = null;
selAvailableLength = frmSelect.selAvailable.length;
i--;

}
}
No; no, it is not needed. You should not have hundreds of thousands
of items in a list box. If you do, you're not factoring your database
properly, or you're not helping the user refine their search enough.
Think for a minute about why this might be crashing: say you've got a
select element which contains OPTION elements. I don't really know
off the top of my head exactly how much memory an OPTION element takes
in memory, but ballpark estimate, let's say each OPTION element takes
about 512B of memory. 512B * 100000 = 51,200,000B = ~50MB of memory
your select box is demanding. Now, since you're iterating, and adding
up to 100000 items at once, the browser is going to have to loop
through - try, just for kicks, running a bare "for" loop that goes
from 0 to 100000 without doing anything, and see how long that takes.
Now add in per iteration time of allocating 512B, instantiating the
DOM object (which takes additional temporary overhead) and adding it
to the DOM, you're looking at some major overhead here.

You should re-think the context of your webpage; think about some
better data organization, such as tree organization, that only loads a
portion of the total at a time. Your employer may feel this is
necessary, but they need to be dissuaded from this foolhardy idea.

-David

Feb 23 '07 #3
Thank you I will try those suggestions,

The most there will be in a list box will be hundreds OR thousands,
not hundreds of thousands, I agree that anything over 5,000 would be
ridiculous which is about the most the website would ever populate,

thanks

Feb 23 '07 #4
On Feb 23, 1:55 pm, "g4toloc0" <seba.gatol...@gmail.comwrote:
On Feb 23, 5:06 am, JuliaRBar...@gmail.com wrote:
I have looked everywhere I can for a solution to this problem but
nothing so far has worked.

mmm.. have you tried with different browsers/OS?
What browser are you currently using?

About the code..

i-- inside the for loop is pure evil..
anyway.. i suppose that your code works so you can try:

use "display" property to hide the destination combo object (that's
really different than using the visibility prop)
try to delay the insert for loop using a setTimeout call.

hope this helps.
Ciao.
Seba
The i--; was a quick fix for adding of items without skipping any, why
is it pure evil? Is it just bad practice or does use more memory? I am
a novice here and appreciate your help, thanks!

Julia

Feb 23 '07 #5
Hey, thanks for the suggestion, I ended up calling the add function
recursively by the setTimeout method with an interval of 1, just
enough to keep it from freezing up, thanks!

Feb 23 '07 #6
On Feb 24, 7:41 am, JuliaRBar...@gmail.com wrote:
On Feb 23, 1:55 pm, "g4toloc0" <seba.gatol...@gmail.comwrote:
On Feb 23, 5:06 am, JuliaRBar...@gmail.com wrote:
I have looked everywhere I can for a solution to this problem but
nothing so far has worked.
mmm.. have you tried with different browsers/OS?
What browser are you currently using?
About the code..
i-- inside the for loop is pure evil..
anyway.. i suppose that your code works so you can try:
use "display" property to hide the destination combo object (that's
really different than using the visibility prop)
try to delay the insert for loop using a setTimeout call.
hope this helps.
Ciao.
Seba

The i--; was a quick fix for adding of items without skipping any, why
is it pure evil? Is it just bad practice or does use more memory?
Because it is causing your browser to crash. You are incrementing i
in the for statement, then decrementing it inside the for block. It
is initially set to zero, decremented to -1 at the bottom of the
block, then incremented back to zero at the start of the next loop -
it will never get beyond zero, you are adding option[0] a zillion
times until the browser crashes.

The options collection is live, so if you *remove* option[i] then
option[i+1] you will skip every second option. However, you are
copying so that isn't an issue.

Just get rid of that line.

Incidentally, if you don't need to copy the options, try just moving
them instead (where you will have to account for skipping options):

var fromSel = frmSelect.selAvailable;
var toSel = frmSelect.selSelected;
var i = 0;
var len = fromSel.length;
while (len--) {
toSel.options[i] = fromSel.options[0];
i++;
}
Untested, but the idea is to simply assign the options from one select
to the other. When you remove fromSel.options[0], they all 'shuffle
up' so what was option[1] is now option[0].
--
Rob

Feb 23 '07 #7
On Feb 24, 12:46 am, JuliaRBar...@gmail.com wrote:
Hey, thanks for the suggestion, I ended up calling the add function
recursively by the setTimeout method with an interval of 1, just
enough to keep it from freezing up, thanks!
I am happy it is working now.. as a suggestion think about this..
when you start a very intensive and long loop, your browser will just
sit down and loop..
it won't run the screen refresh cycle and it won't listen to any mouse
or keyboard event.

Your browser (my little experience is limited to javascript on FF and
IE) will try to inform you that something bad is happening and will
try to bring up an alert window allowing the user to abort the
script.
[this is so easy to try out.. just write something like "while (1)" as
a little demo]

But in certain situations the 'Hang detector alert' is not raised
(this control has been maily implemented for infinite loops
detection..) in addtion, if your loop uses a lot of memory some
problems may occur.
If the trash collector is not able to deallocate memory your script
will be out of control and in a short time your browser will crash.

This explains why using a timer solves the problem. By calling
setTimeout you allow the browser to proccess events and deallocate
memory while dealing with the insertion loop.

Ok, i think that's enough.

Ciao
Seba

Feb 24 '07 #8
On Feb 23, 10:41 pm, JuliaRBar...@gmail.com wrote:
The i--; was a quick fix for adding of items without skipping any, why
is it pure evil? Is it just bad practice or does use more memory?
Well, RobG and David Golightly answers are far more better than mine,
but i need to do some practice (i really hate not being able to speak/
write fluent English).
(note: I am sure that what i am going to say has been already (better)
discussed here..)

And now.. about that "i--"
I think we cant talk a little about good programming practices..

In my opinion talking about "good" and "bad" code is a very subjective
argument, i think it is not different from talking about "my Firefox
is good your IE is bad", "Perl rocks, PHP sucks" etc., etc. (argh...
this is really going beyond about what i want to say..)

Anyway.. there are some "universal" programming practices you can
apply to any programming language:

About for loops
---
Do not touch the loop counter inside the loop itself. It can bring to
unexpected results.
If you are modifying the loop counter this mean that you don't need a
for loop ("while" and "do while" will suit better)

Of course i am talking about 'normal' for loops.. (you can find any
sort of programming genius doing the weirdest things out there)

In your code is easy to see that a for loop it is not the right
choice...
You are saying the javascript interpreter to increment "i" (that's why
you are using a for loop) and then you are using i--.
Of course you are free to do whatever you want if you just need your
code working.. but doing things like this one greatly reduce code
readability (weird code is hard to debug)

Dom Interaction
---
in your loop you are referencing frmSelect lot of times..
You can greatly reduce browser work and execution time by using local
lookup variables. It is a good practice to always minimize the scope
chain of objects.

e.g.

var x=frmSelect.selAvailable; //more suitable var name needed :)
//x is now a lookup var, use x instead of
//frmSelect.selAvailable inside your loop

Ok.. i think this is enough for now.

Ciao
Seba

Feb 24 '07 #9

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

Similar topics

1
by: Amit Shinde | last post by:
I have multiple list box added to the asp page using <select> tag I have added items in the list box using recrdset from Education table Education table={educationid, educationname} User can...
3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
2
by: Neil Ginsberg | last post by:
I'm having a problem with a multi-select list box set to Simple multi-selection. If multiple items are selected and then I change the items in the list, the list positions previously selected are...
14
by: JK Peck | last post by:
I have a fairly large Access application that ran correctly in Access 2000. After upgrading to Access 2003 (and recompiling and updating references), it reliably crashes at a certain point. If I...
9
by: zacks | last post by:
I have written a serialized class that has several properties that are typed as a list of type class. When I deserialize an XML file, the list is populated just fine. But I am having trouble...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
5
by: oceanspell | last post by:
I made a linked list that can add an item, go to the first item, and get the current, next, and previous item in the list. When I compile it, there are no errors, but it overloads. Here is the...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
2
by: DippyDog | last post by:
A long time ago, (yes, yes, in a galaxy yadda yadda yadda) I created a class which inherits List(Of CustomObj) and I needed to recalculate some things whenever an item is added to or removed from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.