Unexplainable VB.NET Error (Can't Modify Items Collection)

  • Thread starter Thread starter Will Chamberlain
  • Start date Start date
W

Will Chamberlain

I am running into a strange error when I try debugging my current
application. A form loads up with a dynamically-populated textbox full
of software titles. Upon selection of a software title and pressing 'GO'
a checkboxlist is displayed with the users who use the current software.
The checkboxlist is bound to a dataset named "DataTable" and has the
DisplayMember set to "Employee" and the DataValue set to "ID", like the
following:

----
Dim strSoftwareTitle As String = comboSoftwareTitles.Text()

Dim objConn As New OleDb.OleDbConnection
objConn.ConnectionString =
System.Configuration.ConfigurationSettings.AppSettings("strConnection")

Dim strSQL As String = "SELECT ID, EmployeeName FROM tblSoftware WHERE "
& _
"(SoftwareTitle = '" & strSoftwareTitle & "') AND " & _
"(EmployeeName Is Not Null) ORDER BY EmployeeName ASC;"

Dim ds As New DataSet
Dim objCommand As New OleDb.OleDbCommand(strSQL, objConn)
Dim myCommand As New OleDb.OleDbDataAdapter(strSQL, objConn)

myCommand.Fill(ds, "DataTable")

Me.clBox.DataSource = ds.Tables("DataTable")
Me.clBox.MultiColumn = False
Me.clBox.DisplayMember = "EmployeeName"
Me.clBox.ValueMember = "ID"
----

When I run the application the first software title in the list happens
to be "Adobe Acrobat 6.01 Professional." When I hit 'go' I get the error
below:

----
Additional information: Cannot modify the Items collection
when the DataSource property is set.
----

The line that is highlighted in debug mode is the following:

----
Me.clBox.ValueMember = "ID"
----

If I quit and run the program again and scroll down and select "Visual
NET 2003 Enterprise" and press 'GO', the checkboxlist displays
flawlessly. What could cause my application to error upon selection of
the first selection, but not another one in the list. In the subroutine
that errors I am not modifying the Items collection. If this isn't clear
enough I can provide more information.

Any suggestions?

- Will
 
Here is the exception Text:


************** Exception Text **************
System.ArgumentException: Cannot modify the Items collection when the
DataSource property is set.
at System.Windows.Forms.ListBox.CheckNoDataSource()
at System.Windows.Forms.ListBox.Sort()
at System.Windows.Forms.ListBox.EndUpdate()
at System.Windows.Forms.ListBox.SetItemsCore(IList value)
at System.Windows.Forms.ListControl.DataManager_ItemChanged(Object
sender, ItemChangedEventArgs e)
at System.Windows.Forms.ListControl.SetDataConnection(Object
newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_ValueMember(String value)
at System.Windows.Forms.CheckedListBox.set_ValueMember(String value)
at SoftwareLicenseProject.Form1.btnSoftwareSearch_Click(Object sender,
EventArgs e) in
C:\Inetpub\wwwroot\SoftwareLicenseProject\SoftwareLicenseProject.vb:line
1108
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
I fixed it but I can't explain it.

All I had to do was change the order of the following:

Me.clBox.DataSource = ds.Tables("DataTable")
Me.clBox.MultiColumn = False
Me.clBox.DisplayMember = "EmployeeName"
Me.clBox.ValueMember = "ID"

To the following:

Me.clBox.ValueMember = "ID"
Me.clBox.DisplayMember = "EmployeeName"
Me.clBox.DataSource = ds.Tables("DataTable")
Me.clBox.MultiColumn = False

Any possible explanations of why or how this works? Some buggy aspect of
the .NET framework is the only thing I can think of. No other reason why
it would work sometimes and not others.
 
Hi,

Will Chamberlain said:
Here is the exception Text:


************** Exception Text **************
System.ArgumentException: Cannot modify the Items collection when the
DataSource property is set.
at System.Windows.Forms.ListBox.CheckNoDataSource()
at System.Windows.Forms.ListBox.Sort()
at System.Windows.Forms.ListBox.EndUpdate()
at System.Windows.Forms.ListBox.SetItemsCore(IList value)
at System.Windows.Forms.ListControl.DataManager_ItemChanged(Object
sender, ItemChangedEventArgs e)
at System.Windows.Forms.ListControl.SetDataConnection(Object
newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
at System.Windows.Forms.ListControl.set_ValueMember(String value)
at System.Windows.Forms.CheckedListBox.set_ValueMember(String value)
at SoftwareLicenseProject.Form1.btnSoftwareSearch_Click(Object sender,
EventArgs e) in
C:\Inetpub\wwwroot\SoftwareLicenseProject\SoftwareLicenseProject.vb:line
1108
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

According to the callstack the problem is caused because
CheckedListBox.Sorted = True and apparently it can't be True when it is
databound.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconsortinglist.asp

Dim dv As DataView = ds.Tables("DataTable").DefaultView
dv.Sort = "EmployeeName"

Me.clBox.Sorted = False
Me.clBox.DataSource = dv
Me.clBox.MultiColumn = False
Me.clBox.DisplayMember = "EmployeeName"
Me.clBox.ValueMember = "ID"


HTH,
Greetings
 
Back
Top