An "An unhandled exception of type 'System.ArgumentException' occurred" error occurs when you set the Sorted property to True

Article translations Article translations
Article ID: 316568 - View products that this article applies to.
This article was previously published under Q316568
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Data
  • System.Data.SqlClient
Expand all | Collapse all

On This Page

SYMPTOMS

When you set the Sorted property of a ListBox control to True after you bind it to a datasource, you may receive the following exception:
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll.

Additional information: Can't modify the Items collection when the DataSource property is set.

RESOLUTION

To resolve this issue, set the Sorted property before you bind the ListBox to a datasource.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET.
  2. Create a new Windows application in Visual Basic .NET. Form1 is added to the project by default.
  3. Make sure that your project contains a reference to the System.Data namespace; if it does not, add a reference to this namespace.
  4. Place a Button control on Form1. Change the Name property of the button to btnTest, and then change the Text property to Test.
  5. Add a ListBox control to Form1.
  6. Use the Imports statement on the System and System.Data namespaces so that you are not required to qualify declarations in those namespaces later in your code. Add the following code to the "General Declarations" section of Form1:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    					
  7. Copy and paste the following code in the btnTest_Click event:
    Dim ds As New DataSet()
    Dim con As New SqlConnection("server=myServer;integrated security=sspi;database=northwind")
    Dim da As New SqlDataAdapter("select contactname from customers", con)
    da.Fill(ds, "cname")
    'ListBox1.Sorted = True 'Uncomment to sort the items.
    ListBox1.DataSource = ds.Tables(0)
    ListBox1.DisplayMember = "contactname"
    ListBox1.Sorted = True  'Comment to sort the items without error
    					
  8. Modify the connection string as appropriate for your environment.
  9. Save your project. On the Debug menu, click Start to run your project.
  10. Click Test.

    You receive the error message described in the "Symptoms" section.
  11. Uncomment the line after "da.Fill," and then comment the last line in the code section.
  12. Save and run your project.

    The ListBox is sorted properly.

REFERENCES

For more information about the ListBox.Sorted property and other properties pertaining to the ListBox class, browse to the following MSDN Web sites:
ListBox.Sorted property
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.sorted(vs.71).aspx
ListBox class
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox(vs.71).aspx

Properties

Article ID: 316568 - Last Review: December 6, 2006 - Revision: 2.7
APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 2.0
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbtshoot kberrmsg kbcontrol kbdatabinding kblistbox kbprb KB316568

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com