473,609 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 selAvailableLen gth = frmSelect.selAv ailable.length
var selSelectedLeng th = frmSelect.selSe lected.length
var newoption
var count = selSelectedLeng th + 0;

for (i = 0; i < selAvailableLen gth; i++)
{
selectedText = frmSelect.selAv ailable.options[i].text;
selectedValue = frmSelect.selAv ailable.options[i].value;
newoption = new Option(selected Text, selectedValue, false, false);
frmSelect.selSe lected.options[count] = newoption;
count = count + 1;
frmSelect.selAv ailable.options[i] = null;
selAvailableLen gth = frmSelect.selAv ailable.length;
i--;
}

}

Feb 23 '07 #1
8 1787
On Feb 23, 5:06 am, JuliaRBar...@gm ail.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...@gm ail.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 selAvailableLen gth = frmSelect.selAv ailable.length
var selSelectedLeng th = frmSelect.selSe lected.length
var newoption

var count = selSelectedLeng th + 0;

for (i = 0; i < selAvailableLen gth; i++)
{
selectedText = frmSelect.selAv ailable.options[i].text;
selectedValue = frmSelect.selAv ailable.options[i].value;
newoption = new Option(selected Text, selectedValue, false, false);
frmSelect.selSe lected.options[count] = newoption;
count = count + 1;
frmSelect.selAv ailable.options[i] = null;
selAvailableLen gth = frmSelect.selAv ailable.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...@gm ail.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...@gm ail.com wrote:
On Feb 23, 1:55 pm, "g4toloc0" <seba.gatol...@ gmail.comwrote:
On Feb 23, 5:06 am, JuliaRBar...@gm ail.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.selAv ailable;
var toSel = frmSelect.selSe lected;
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...@gm ail.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...@gm ail.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.sel Available; //more suitable var name needed :)
//x is now a lookup var, use x instead of
//frmSelect.selAv ailable 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
1301
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 choose more than items from the list box. These items gets added in the user table in field "edu" seprated by commas. When User wants to edit his information ...i want that the items in the list box should be automatically selected which are...
3
6800
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 a member function of the same class I get a memory access violation. For example: // this is fine, no error void CBase::FuncA( void ) { plugin_info_t *plugin_info = new plugin_info_t;
2
2713
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 still selected. For example, if the list contains a, b, c, d, and e, and b and c are selected, and then the list is changed to contain v, w, x, y, z, the w and x items are selected. This doesn't happen every time; only intermittently. Even...
14
5258
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 step through the VBA code, the crash does not occur. What is different about stepping through code instead of just running it? Any idea how to find the cause? I know about where it happens, but since it is Access itself crashing, finding a...
9
1517
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 manually loading the class for serialization when the class has not been initialized by a deserialize. If I add three instances of a class to the list, each with different values for their properties, the XML file created by serialization has three...
6
2158
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. === PEP: xxx Title: Unification of for-statement and list-comprehension syntax
5
1419
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 code: //fnamelink.h------------------------------------------------------ class flink { public: char data; flink* previous;
7
2825
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 to support cloning: Public Class RefClass Public tcp As TcpClient Public name As String End Class Public Class RefClassList Inherits List(Of RefClass) Implements ICloneable
2
5559
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 the list. My first attempt was to override the .Add() and .Remove() methods, but they are not declared as 'Overridable.' So I Shadowed them instead, calling in each the same method from MyBase and then calling a ListChanged() method of my own in...
0
8139
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8232
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6064
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.