I have two arraylists. The JobArraylist gets adresses from a database table and JobIDArray Gets the ID for those addresses.
I'm populating a Combobox with the JobArrayList which is working ok.
When I select a an address in the combobox I use the sectectindex to get the AdressID from JobIDArray.
My Issue is when I have duplicate addresses the JobIDArray always returns the the ID for the last Address. not the select one.
Thank you
this code gets data from the database and populates the Combobox.

Private Sub PopulateSearch()
        cbxSearch.Items.Clear()
        Dim JobArraylist As New ArrayList
        Dim Query As String = "SELECT JobID,JobAddress FROM JobName_tbl Order by JobAddress"
        Dim CFIConnection As New Connection
        CFIConnection.FilePath = "C:\CFI\FilePath.Txt"
        CFIConnection.connect()
        Dim cmd As New OleDb.OleDbCommand(Query, CFIConnection.con)
        cmd.CommandType = CommandType.Text
        cmd.CommandText = Query
        CFIConnection.con.Open()

        Dim dr As OleDb.OleDbDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            If Not dr.IsDBNull(1) Then
                JobArraylist.Add(dr.GetString(1)) ' arraylist will be used to populate combobox
                JobIDArray.Add(dr.GetInt32(0)) 'When the Selected Item on the Combobox is Selected it gets the Item ID
            End If
        End While
        For Each Job In JobArraylist
            cbxSearch.Items.Add(Job)
        Next
        dr.Close()
        CFIConnection.con.Close()
    End Sub

This Code gets the ID for the selected Address.

Private Sub cbxSearch_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxSearch.SelectedIndexChanged
 Dim x As Integer = cbxSearch.SelectedIndex
        If cbxSearch.SelectedItem IsNot Nothing Then
            NewCustomerID = CInt(JobIDArray.Item(x))
            GridLoad = True
            UpdateAddress = True
            btnAddJob.Enabled = False
            LoadListGroup()
            btnCustom.Enabled = True
            Dim JOBSearch As New JobAddress
            JOBSearch.Address = cbxSearch.SelectedItem.ToString
            JOBSearch.NewSearch()
End Sub

You don't need to have two arraylists. Here is an example, how it would be better:

Private Sub pop_combo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		Dim Jobs As New Dictionary(Of Integer, String)

		'instead of this loop you do your data reading here
		For i As Integer = 1 To 10
			Jobs.Add(i, "Job " & i)
		Next

		cbxSearch.DisplayMember = "Value"
		cbxSearch.ValueMember = "Key"
		cbxSearch.DataSource = Jobs.ToList
	End Sub

	Private Sub cbxSearch_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cbxSearch.SelectedIndexChanged
		'get the selected JobID
		MsgBox(String.Format("Selected jobID = {0}", CType(cbxSearch.SelectedItem, KeyValuePair(Of Integer, String)).Key))
	End Sub

Thanks GeekByChoice

It works great.
The dictionary object is something new to me.
I will be looking into it more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.