Error - Cannot modify the items collection...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello
I already posted this question, but received no response
I'll try to word it better :

I have a form with a lisbox (LBox1), a button, that when pressed populates LBox1, and a tab control with 2 pages
On the 2nd tab page is another listbox (LBox2), whose values are based on what is selected in LBox1
If I run the project, and go directly to the 2nd tab page, then populate LBox1, everything works great
If I populate LBox1 first, then click on the 2nd tab page, I get the error message
"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dl
Additional information: Cannot modify the Items collection when the DataSource property is set.

The code that triggers the error is in the following sub

Private Sub get_all_CBs(
lBox2.datasource = Nothin
lBox2.items.Clear(
lBox2.Refresh(

Dim DI2 as String = txtCPID.Tex
Dim ConStr2 as String = "...my connection string...
Dim dsACB as DataSet = SqlHelper.ExecuteDataset(ConStr2, CommandType.Text, ...my SQL code...
lBox2.DataSource = dsACB.Tables(0) <<----- this is the line that triggers the error..
lBox2.DisplayMember = "STR_BLOCK_NUMBER

Please help!!

Thanks in advance
Ambe
 
Hi Amber,

I do not see it, but try this
Private Sub get_all_CBs()
lBox2.datasource = Nothing
lBox2.items.Clear()
lBox2.Refresh()
Delete the three rows above
Dim DI2 as String = txtCPID.Text
Dim ConStr2 as String = "...my connection string..."
Dim dsACB as DataSet = SqlHelper.ExecuteDataset(ConStr2,
CommandType.Text, ...my SQL code...)

Lbox2.datasource = Nothing ' and not more
lBox2.DataSource = dsACB.Tables(0) <<----- this is the line that triggers the error...
lBox2.DisplayMember = "STR_BLOCK_NUMBER"



Cor
 
Thanks for the reply
This may sound bizarre, but at first it seemed to work, then after trying it a few times...it started giving me the error message again
I'm truly baffled here...
ambe
 
Amber

Are you sure you are not trying to add an item in that listbox somewhere
else?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top