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


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

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


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


This behavior is by design.


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.


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
ListBox class


Article ID: 316568 - Last Review: December 6, 2006 - Revision: 2.7
  • 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
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