473,322 Members | 1,409 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.

Combobox fun - "Specified argument was out of the range..."

Hi... Synposis...

Throws exception:
"Specified argument was out of the range of valid values."
Read on for the juicy tidbits.

MySimpleClassCol mscc=new MySimpleClassCol();

private void InitCombo()
{
// Doing this so bind op doesn't fire event
m_combo.SelectedIndexChanged -= new System.EventHandler...
mscc.Clear();
XmlNodeList nl=m_xml.DocumentElement.SelectNodes("/a/b/c/d");
foreach(XmlNode n in nl)
{
// Grab a bunch of strings from the node innerxml
MySimpleClass sc=new MySimpleClass(s1, s2, s3, s4);
mscc.Add(sc);
}
m_combo.DataSource=null;
m_combo.DataSource=mscc;
m_combo.DisplayMember="ComboName";
m_combo.SelectedIndex=-1;
m_combo.SelectedIndex=-1;
m-combo.SelectedIndexChanged += new System.EventHandler...
try
{
if(m_combo.Items.Count>0)
m_combo.SelectedIndex=0;
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

InitCombo is called on Form load. It is also called whenever a change
is made to the underlying XML document.

Scenario 1:
1. Add a <d> node to the XML document
2. Save XML document to disk
3. InitCombo()
4. m_combo.SelectedIndex=m_combo.FindStringExact(newV alue);

This works.

Scenario 2:
1. Remove an inner <d> XML node - NOT the last one added
2. Save XML document to disk
3. InitCombo()
4. Leave m_combo.SelectedIndex=0;

This works.

Scenario 3:
1. Remove the last XML node added - always last child of parent node <c>
2. Save XML document to disk
3. InitCombo()

THIS BLOWS UP on:

try
{
if(m_combo.Items.Count>0) // which it is...
m_combo.SelectedIndex=0; // Throws exception
}

The exception thrown is:
"Specified argument was out of the range of valid values.
Parameter name: Index was out of range.
Must be non-negative and less than the size of the collection."

Nice... except that .Count is non-zero, 0 is non-negative, and less than
the size of the collection. And it ONLY happens when I remove the last child
in the XML... which corresponds to the last item in the list.

<a>
<b>
<c>
<d name="OKToDelete">
</d>
<d name="OKToDelete">
</d>
<d name="OKToDelete">
</d>
<d name="BLOWS_UP_ONDelete">
</d>
</c>
</b>
</a>

I have fiddled with the event handlers. I've tried NOT rebinding and just
repopulating the data set. I have all but waved a dead chicken over
the keyboard.

Could anyone throw me a bone here?

Thanks,
M.

Nov 16 '05 #1
3 3451
Michael,

The combobox fun is mostly when you change an index in an indexchange event.
That fires it again in the middle. The in my opinion most simple solution
than. Start event with removing the Event.Handler and add it again leaving
that event..

(I don't see that event)

I hope this helps something?

Cor
Nov 16 '05 #2
Cor,

Thanks for the response. I tried disabling the event handler, but it made
very little difference. Now instead of the form disappearing due to the
exception, the combobox refills with Class.Property. The text box of the
control displays Class.Property, but the actual dropdown list shows the
correct items. Clicking on any one of them yields the exception:

ArgumentOutOfRange

on every line past ***

m_combo.DataSource=null;
m_combo.DataSource=m_bcs;
***
m_combo.DisplayMember="ComboName";
m_combo.SelectedIndex=-1;

On break, m_bcs shows .Count=4. ToString() clearly shows "ComboName".

What's more puzzling than anything is why this works on everything except
the removal of the last element in m_bcs. Remove or add anything anywhere
else and there are NO issues. I have a suspicion it's more to do with the
databinding process than fiddling with the indices, although I did try
disabling/reenabling the event handler in the combobox, and in other items
that are influenced by the combo box... no help. Go fish?

Cheers,
M.

"Cor Ligthert" wrote:
Michael,

The combobox fun is mostly when you change an index in an indexchange event.
That fires it again in the middle. The in my opinion most simple solution
than. Start event with removing the Event.Handler and add it again leaving
that event..

(I don't see that event)

I hope this helps something?

Cor

Nov 16 '05 #3
Michael,

Disabling the last item in a collection does not give only with a combobox
often problems.
That is the reason why in loops this is often done from the last to the
first.

Cor
Nov 16 '05 #4

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

Similar topics

0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
0
by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried to run it by hitting "F5". I got an exception:...
0
by: XmlAdoNewbie | last post by:
Hello All, I have come across something that seems a big buggy to me. I have an app with about 15 comboboxes on it. The comboboxes are loaded with name/value pairs at run time. Sometimes i will...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
2
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP...
0
by: RCM | last post by:
I am really eager to quit the Microsoft world and get into LAMP. For the immediate future I can't do the Apache part, so I am at least trying to get PHP up and running under IIS to learn it. I...
0
by: gsauns | last post by:
I have a DetailsView which is inside of a FormView. I lifted this DetailsView straight off another one of my pages, where it was working beautifully. Now I get this endlessly frustrating error...
2
by: RONSCHERER | last post by:
Hi. I write in VB. For some strange reason, throughout all the code written in one of my workbooks, the word "Range" had simply changed its case to "rANGE". eg: rANGE("C11").Copy ...
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
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: 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
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.