T
Tim
Hi,
I'm very new to .NET and am programming in C#. I have a web application
where i have two list boxes. Its kind of like a shopping card where you can
add items from one 'locations' list box to the 'locations selected' (cart)
listbox. I have a hirarchy of locations - main locations and sub
locations. main locations being the parent locations, and the sub locations
being the child locations.
I want to be able to check to see if the already selected locations listbox
(cart) already contains a child location of a parent location that is trying
to be added to the collection (because there is no need to add the child
there if the parent is selected). if so the child should be deleted and the
parent added
I am looping through the locations selected, and for each one looping
through the already selected (cart) locations collection and checking the
existance of the members.
here is the code:
private void Button1_Click(object sender, System.EventArgs e)
{
if(lstLocations.SelectedIndex != -1)
{
foreach(ListItem selectedLocation in lstLocations.Items)
{
if(selectedLocation.Selected)
{
bool itemAlreadyExists = false;
bool parentItemExists = false;
bool childItemExists = false;
foreach(ListItem locationsSelected in
lstCart.Items)
{
//does new location already exist in
locations collection?
if(selectedLocation.Text.Equals(locationsSelected.Text.ToString()))
{
itemAlreadyExists = true;
lblNotice.Text += "At least one of
the selected items already exists and has been ignored<br>";
}
if(!itemAlreadyExists)
{
//does parent of child already exist
in collection
if(selectedLocation.Value.StartsWith(locationsSelected.Value.ToString()+"-")
)
{
parentItemExists = true;
lblNotice.Text += "You
attempted to add a child location of a parent already selected. There is no
need to add a child location when the parent is already in your collection.
Item ignored<br>";
}
//does child of parent already exist
in collection, if so get rid of it
//this is the bit that is giving the
error!
if(locationsSelected.Value.StartsWith(selectedLocation.Value.ToString()+"-")
)
{
childItemExists = true;
lstCart.Items.Remove(locationsSelected); //this is where the error
occurs
lstCart.Items.Add(selectedLocation);
lblNotice.Text += "A child
location was removed because you added its parent!<br>";
}
}
}
if(!itemAlreadyExists && !parentItemExists &&
!childItemExists)
{
lstCart.Items.Add(selectedLocation);
}
}
}
}
}
when i execute the page and try add a parent location when a child already
exists the page throws the following error:
Collection was modified; enumeration operation may not execute.
I know this is pretty much saying that as the loop progresses, the code
tries to delete a member of the collection and hence modify the loop.
Can you see what i am trying to achieve and please advise me of the best way
around this problem?
Thanks alot for your help
Tim..
I'm very new to .NET and am programming in C#. I have a web application
where i have two list boxes. Its kind of like a shopping card where you can
add items from one 'locations' list box to the 'locations selected' (cart)
listbox. I have a hirarchy of locations - main locations and sub
locations. main locations being the parent locations, and the sub locations
being the child locations.
I want to be able to check to see if the already selected locations listbox
(cart) already contains a child location of a parent location that is trying
to be added to the collection (because there is no need to add the child
there if the parent is selected). if so the child should be deleted and the
parent added
I am looping through the locations selected, and for each one looping
through the already selected (cart) locations collection and checking the
existance of the members.
here is the code:
private void Button1_Click(object sender, System.EventArgs e)
{
if(lstLocations.SelectedIndex != -1)
{
foreach(ListItem selectedLocation in lstLocations.Items)
{
if(selectedLocation.Selected)
{
bool itemAlreadyExists = false;
bool parentItemExists = false;
bool childItemExists = false;
foreach(ListItem locationsSelected in
lstCart.Items)
{
//does new location already exist in
locations collection?
if(selectedLocation.Text.Equals(locationsSelected.Text.ToString()))
{
itemAlreadyExists = true;
lblNotice.Text += "At least one of
the selected items already exists and has been ignored<br>";
}
if(!itemAlreadyExists)
{
//does parent of child already exist
in collection
if(selectedLocation.Value.StartsWith(locationsSelected.Value.ToString()+"-")
)
{
parentItemExists = true;
lblNotice.Text += "You
attempted to add a child location of a parent already selected. There is no
need to add a child location when the parent is already in your collection.
Item ignored<br>";
}
//does child of parent already exist
in collection, if so get rid of it
//this is the bit that is giving the
error!
if(locationsSelected.Value.StartsWith(selectedLocation.Value.ToString()+"-")
)
{
childItemExists = true;
lstCart.Items.Remove(locationsSelected); //this is where the error
occurs
lstCart.Items.Add(selectedLocation);
lblNotice.Text += "A child
location was removed because you added its parent!<br>";
}
}
}
if(!itemAlreadyExists && !parentItemExists &&
!childItemExists)
{
lstCart.Items.Add(selectedLocation);
}
}
}
}
}
when i execute the page and try add a parent location when a child already
exists the page throws the following error:
Collection was modified; enumeration operation may not execute.
I know this is pretty much saying that as the loop progresses, the code
tries to delete a member of the collection and hence modify the loop.
Can you see what i am trying to achieve and please advise me of the best way
around this problem?
Thanks alot for your help
Tim..