Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hi I used below code for filling my combo box with a dataset, but when I want to set this combo box with new dataset in the second time I got this error

 

"Can't modify the items collection when the datasource property is set."

 

how can I release my combo box from previous datasource to setting new datasource?

 

 

 

please help me

thanks

 

 

P.S. this is my code:

Public Sub DoFillComboWithValues(ByRef MyComboBox As                ComboBox, ByVal strStoredProcedureName As String,         ByVal shoTableID As Short, ByVal strValueMember As           String, ByVal strDisplayMember As String)
     
 Dim ds As DataSet = SqlHelper.ExecuteDataset(g_strConnection, CommandType.StoredProcedure, strStoredProcedureName, New SqlParameter("@TableID", shoTableID))
       Dim dtBasicValues As DataTable = ds.Tables(0)
       Dim dvBasicValues As DataView = dtBasicValues.DefaultView

       dvBasicValues.Sort = "Basic_Default"
       Dim foundIndex As Integer = dvBasicValues.Find(1)

       With MyComboBox
           .Items.Clear()
           .ValueMember = strValueMember
           .DisplayMember = strDisplayMember
           .DataSource = dvBasicValues
       End With

   End Sub

Edited by Robby
  • *Experts*
Posted

I can't be certain, but you may want to try removing that .Items.Clear()

line from there. I don't have a test project set up at the moment with

a DB connection, so I can't test it, but if that's the error you're getting,

I would be inclined to think that perhaps that is the errornous line.

If the combo is bound to a dataset, and you can't modify the Items collection, then you can't clear the Items collection as you are

doing in your code.

Posted (edited)

Thanks for your reply; but I changed my code but in the line of

".DataSource = Nothing" I got previous error;

I tell you my problem again:

first time I call 'DoFillComboWithValues' routin I don't get any error but when I call it in the second time I got this error in the

'.DataSource = dvBasicValues' line, after I changed my code as you ask me, I got this error over ".DataSource = Nothing" line

 

I think I must set dataset to nothing ( as you told me ) but I don't know how can I do it?

 

Public Sub DoFillComboWithValues(ByRef MyComboBox As

ComboBox, ByVal strStoredProcedureName As String, ByVal shoTableID As Short, ByVal strValueMember As String, ByVal strDisplayMember As String)

Dim ds As DataSet = SqlHelper.ExecuteDataset(g_strConnection, CommandType.StoredProcedure, strStoredProcedureName, New SqlParameter("@TableID", shoTableID))

Dim dtBasicValues As DataTable = ds.Tables(0)

Dim dvBasicValues As DataView = dtBasicValues.DefaultView

 

dvBasicValues.Sort = "Basic_Default"

Dim foundIndex As Integer = dvBasicValues.Find(1)

 

With MyComboBox

.Items.Clear()

.ValueMember = strValueMember 'second time that I call this routin I got error here

.DisplayMember = strDisplayMember

.DataSource = dvBasicValues

End With

 

'I added your sugestion here

 

Public Sub DoFillComboWithValues(ByRef MyComboBox As

ComboBox, ByVal strStoredProcedureName As String, ByVal shoTableID As Short, ByVal strValueMember As String, ByVal strDisplayMember As String)

Dim ds As DataSet = SqlHelper.ExecuteDataset(g_strConnection, CommandType.StoredProcedure, strStoredProcedureName, New SqlParameter("@TableID", shoTableID))

Dim dtBasicValues As DataTable = ds.Tables(0)

Dim dvBasicValues As DataView = dtBasicValues.DefaultView

 

dvBasicValues.Sort = "Basic_Default"

Dim foundIndex As Integer = dvBasicValues.Find(1)

 

With MyComboBox

.Items.Clear()

.DataSource = Nothing 'second time I got eror here

.ValueMember = strValueMember

.DisplayMember = strDisplayMember

.DataSource = dvBasicValues

End With

Edited by gfard

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...