I received the following error "Items collection cannot be modified when the DataSource property is set" when populating the combo box with data table.
Reason:
The Items.Clear method was called while the combo box's data source was assigned.
Solution:
Need to set the combo box's data source to nothing before calling Items.Clear method
The following sample code explains this scenario clearly.
Private Sub populatedata()
Dim SQLdta As SqlDataAdapter = Nothing
Try
Dim SQl As String = "SELECT AlertID, alertName FROM alert_Master"
SQLdta = New SqlDataAdapter(SQl, SConnectionString)
Dim dt As New DataTable
SQLdta.Fill(dt)
cmbProducts.DataSource = Nothing // Need to set the data source nothing
// Comment this line to reproduce the error
cmbtype.Items.Clear()
cmbtype.DataSource = dt
cmbtype.DisplayMember = "Name"
cmbProducts.ValueMember = "ID"
Catch ex As Exception
msgbox(ex.Message)
End Try
End Sub
Reason:
The Items.Clear method was called while the combo box's data source was assigned.
Solution:
Need to set the combo box's data source to nothing before calling Items.Clear method
The following sample code explains this scenario clearly.
Private Sub populatedata()
Dim SQLdta As SqlDataAdapter = Nothing
Try
Dim SQl As String = "SELECT AlertID, alertName FROM alert_Master"
SQLdta = New SqlDataAdapter(SQl, SConnectionString)
Dim dt As New DataTable
SQLdta.Fill(dt)
cmbProducts.DataSource = Nothing // Need to set the data source nothing
// Comment this line to reproduce the error
cmbtype.Items.Clear()
cmbtype.DataSource = dt
cmbtype.DisplayMember = "Name"
cmbProducts.ValueMember = "ID"
Catch ex As Exception
msgbox(ex.Message)
End Try
End Sub
No comments:
Post a Comment