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

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Michael Conroy (MichaelConroy_at_discussions.microsoft.com)
Date: 02/03/05


Date: Thu, 3 Feb 2005 08:31:04 -0800

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(newValue);

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.



Relevant Pages

  • Combobox exception - xpost/C# forum
    ... MySimpleClassCol mscc=new MySimpleClassCol; ... InitCombo is called on Form load. ... is made to the underlying XML document. ... Remove the last XML node added - always last child of parent node ...
    (microsoft.public.dotnet.general)
  • Passing XML as a parameter to a WS.
    ... I've to pass one xml node at a time to a Web Service. ... The Web Service will create an xml document and then will validate it ...
    (microsoft.public.dotnet.languages.csharp)