Combo box and datasource

gfard

Member
Joined
Apr 9, 2003
Messages
11
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

"Cant 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:
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
 
Last edited by a moderator:
I cant be certain, but you may want to try removing that .Items.Clear()
line from there. I dont have a test project set up at the moment with
a DB connection, so I cant test it, but if thats the error youre getting,
I would be inclined to think that perhaps that is the errornous line.
If the combo is bound to a dataset, and you cant modify the Items collection, then you cant clear the Items collection as you are
doing in your code.
 
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 dont 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 dont 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
 
Last edited by a moderator:
Back
Top