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



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.Net 2.0
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic 2005

This article was previously published under Q316568
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Data
  • System.Data.SqlClient

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:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbtshoot kberrmsg kbControl kbDataBinding kbListBox kbprb KB316568 kbAudDeveloper