Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox Index out of range

Status
Not open for further replies.

spidervenom

Programmer
Mar 8, 2003
19
0
0
US
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


 
You can use the combobox properties to select the item that you want by using the combobox1.SelectedText = "MyText"

I also wouldn't necessarily create the combobox with a blank entry, I would simply set the property to Disabled (combobox.Enabled = False) so that the user couldn't actually select anything, then handle your previous comboboxes SelectedIndexChanged Event and enable the dependant combobox once the previous selection was validated.

Your error is produced because you are mixing up zero and 1 based arrays



 
Thanks for the help.

Just want to clarify, the data from combobox1 is from DataSource. Thus, by default, the first item displays in combobox1 will be the first item from the DataTable.

When the form starts, a blank item is added to combobox1 because we want to force the user to click on combobox1 to select an item from the dropdown (to avoid selecting the default). By selecting from combobox1, it will enable combobox2.

I know I can disable the combobox2 (combobox2.Enabled = False) based on the condition when combobox1 is blank, but how can I display combobox1 without adding a blank item at form load?

Also, what should I change in the code fix the array mixed up?

Thanks a lot!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top