spidervenom
Programmer
Hi everyone,
I have two comboboxes (Sample and Lot). Both get data from DataSource. The items in Lot combobox will populate based on the selection in Sample. I have added a blank line to the combobox so that the user will not see the data right away when the form is load (they must select the dropdown from the combobox to see it).
When I select a Sample, sometimes the Lot combobox is blank and get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Is it some problem within my code or it's a bug from Microsoft? Any suggestions will help..many thanks!!
Here is what I did to add a blank line for the Sample combobox:
'add blank row in SampleDS
dsrow = SampleDS.Tables("Sample").NewRow
dsrow(1) = ""
SampleDS.Tables("Sample").Rows.Add(dsrow)
cmbSample.SelectedIndex = cmbSample.FindStringExact("")
...and here is the cmbSample_SelectedIndexChanged event:
Private Sub cmbSample_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSample.SelectedIndexChanged
Dim LotDA As New Odbc.OdbcDataAdapter
' fill the Lot combobox with the constrained Lot numbers
cmbLot.Enabled = False
cmbLot.DataSource = Nothing
cmbLot.Items.Clear()
If Not (cmbSample.Text = "") Then
Dim selSampleId As String = CType(cmbSample.SelectedItem, SamplesVal).Id
'Dim LotDA As New Odbc.OdbcDataAdapter
LotDA.SelectCommand = New Odbc.OdbcCommand
LotDA.SelectCommand.Connection = odbcCon
LotDA.SelectCommand.CommandText = "select distinct b.name as Lot, b.row_id as LotID from stock a, prod b where a.lot_id = b.row_id and a.prdint_id = '" & selSampleId.Trim & "' order by b.name"
LotDS = New DataSet
LotDA.Fill(LotDS, "Lot")
' now create and populate a new DataTable
LotDT.Clear()
Dim newrow As SamplesVal
For Each dsrow As DataRow In LotDS.Tables("Lot").Rows
newrow = New SamplesVal
newrow.Name = dsrow("Lot")
newrow.Id = dsrow("LotID")
LotDT.Add(newrow)
Next
' refresh the combobox association
cmbLot.DataSource = LotDT
cmbLot.DisplayMember = "Name"
cmbLot.Refresh()
cmbLot.Select()
LotDS.Dispose()
LotDA.Dispose()
cmbLot.Enabled = True
Else
cmbLot.Items.Clear()
End If
End Sub
I have two comboboxes (Sample and Lot). Both get data from DataSource. The items in Lot combobox will populate based on the selection in Sample. I have added a blank line to the combobox so that the user will not see the data right away when the form is load (they must select the dropdown from the combobox to see it).
When I select a Sample, sometimes the Lot combobox is blank and get this error:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Is it some problem within my code or it's a bug from Microsoft? Any suggestions will help..many thanks!!
Here is what I did to add a blank line for the Sample combobox:
'add blank row in SampleDS
dsrow = SampleDS.Tables("Sample").NewRow
dsrow(1) = ""
SampleDS.Tables("Sample").Rows.Add(dsrow)
cmbSample.SelectedIndex = cmbSample.FindStringExact("")
...and here is the cmbSample_SelectedIndexChanged event:
Private Sub cmbSample_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbSample.SelectedIndexChanged
Dim LotDA As New Odbc.OdbcDataAdapter
' fill the Lot combobox with the constrained Lot numbers
cmbLot.Enabled = False
cmbLot.DataSource = Nothing
cmbLot.Items.Clear()
If Not (cmbSample.Text = "") Then
Dim selSampleId As String = CType(cmbSample.SelectedItem, SamplesVal).Id
'Dim LotDA As New Odbc.OdbcDataAdapter
LotDA.SelectCommand = New Odbc.OdbcCommand
LotDA.SelectCommand.Connection = odbcCon
LotDA.SelectCommand.CommandText = "select distinct b.name as Lot, b.row_id as LotID from stock a, prod b where a.lot_id = b.row_id and a.prdint_id = '" & selSampleId.Trim & "' order by b.name"
LotDS = New DataSet
LotDA.Fill(LotDS, "Lot")
' now create and populate a new DataTable
LotDT.Clear()
Dim newrow As SamplesVal
For Each dsrow As DataRow In LotDS.Tables("Lot").Rows
newrow = New SamplesVal
newrow.Name = dsrow("Lot")
newrow.Id = dsrow("LotID")
LotDT.Add(newrow)
Next
' refresh the combobox association
cmbLot.DataSource = LotDT
cmbLot.DisplayMember = "Name"
cmbLot.Refresh()
cmbLot.Select()
LotDS.Dispose()
LotDA.Dispose()
cmbLot.Enabled = True
Else
cmbLot.Items.Clear()
End If
End Sub