gfard Posted April 9, 2003 Posted April 9, 2003 (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 April 9, 2003 by Robby Quote
*Experts* Volte Posted April 9, 2003 *Experts* Posted April 9, 2003 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. Quote
Moderators Robby Posted April 9, 2003 Moderators Posted April 9, 2003 Try this With MyComboBox .DataSource = Nothing .DataMember = String.Empty end with Quote Visit...Bassic Software
gfard Posted April 9, 2003 Author Posted April 9, 2003 (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 April 9, 2003 by gfard Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.