title: Take combobox SelectedIndex data and use in SELECT Query - VB.net
body: <p>I'm a little desperate, hence why I'm here! I'm quite new to programming and have been given an assignment where I need to use a range of SQL queries to generate a simple HTML report table. There is also a user input, with them selecting the ClinicID from the comboBox and clicking a button to generate the report.</p>
  
  <p><strong>Basically, I have a comboBox that I have populated with 'ClinicID', as below. I have also made sure that SelectedIndex is working. I need to somehow use this in the SQL query method which I have also provided below.</strong></p>
  
  <pre><code>Public Class frmReport1
  'Set lsData for Clinics table
  Dim lsData As List(Of Hashtable)
  
  
  'On form load
  Private Sub frmReport1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  
      cboClinicID.DropDownStyle = ComboBoxStyle.DropDownList
  
      'Instantiate new ClinicController object
      Dim cController As ClinicController = New ClinicController
  
      'Load ClinicID
      lsData = cController.findId()
  
      For Each clinic In lsData
          cboClinicID.Items.Add(CStr(clinic("ClinicID")))
      Next
  
  End Sub
  
  'Selected Index 
  Private Sub cboClinicID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClinicID.SelectedIndexChanged
      Dim selectedIndex As Integer = cboClinicID.SelectedIndex
      Dim selectedItem As Object = cboClinicID.SelectedItem
  
      'Print in debug window
      Debug.Print("Selected clinicID: " &amp; selectedItem.ToString())
      Debug.Print("Selected clinicID index: " &amp; selectedIndex.ToString())
  
      Dim htData = lsData.Item(selectedIndex)
  
  
  End Sub
  </code></pre>
  
  <p><strong>SQL query method - **note, I'm pulling from two different tables</strong>:</p>
  
  <p>Where the '?' is, is where I assume I have to work in the 'SelectedItem' but I have no idea how!</p>
  
  <p><strong>Desired result: an html table outputted with these three selected fields.</strong></p>
  
  <pre><code>Public Class ClinicOrderController
  
  Public Const CONNECTION_STRING As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PharmDB.accdb"
  
  'Dim cController As ClinicController = New ClinicController
  'Dim oController As OrderController = New OrderController
  
  Public Function findClinicOrder() As List(Of Hashtable)
  
      'Instantiates a connection object
      Dim oConnection As OleDbConnection = New OleDbConnection(CONNECTION_STRING)
      'Instantiates a list of hashtables
      Dim lsData As New List(Of Hashtable)
  
      Try
          Debug.Print("Connection string: " &amp; oConnection.ConnectionString)
  
          oConnection.Open()
          Dim oCommand As OleDbCommand = New OleDbCommand
          oCommand.Connection = oConnection
  
          'Stored in the CommandText property of the command object
          'SELECT SQL statement
          oCommand.CommandText = "SELECT clinics.clinic_id, orders.date_ordered, orders.total_price FROM clinics, orders WHERE clinics.clinic_id = orders.clinic_id AND clinics.clinic_id = ? ORDER BY clinics.clinic_id"
  
          'Compiles the prepared statement
          'oCommand.Prepare()
          'Executes the SQL statement and stores the results in data reader object
          Dim oDataReader = oCommand.ExecuteReader()
  
          'Process data set in Hashtable
          Dim htTempData As Hashtable
          Do While oDataReader.Read() = True
              htTempData = New Hashtable
              htTempData("ClinicID") = CStr(oDataReader("clinic_id"))
              htTempData("DateOrdered") = CStr(oDataReader("date_ordered"))
              htTempData("OrderTotalPrice") = CStr(oDataReader("total_price"))
              lsData.Add(htTempData)
  
          Loop
  
          Debug.Print("The record was found.")
  
      Catch ex As Exception
          Debug.Print("ERROR:" &amp; ex.Message)
          MsgBox("An error occured!")
      Finally
          oConnection.Close()
      End Try
  
      'Return list of hashtables to the calling function
      Return lsData
  
  End Function
  </code></pre>
  
  <p>Really, really appreciate any help here. Ive been struggling with this for more than 8 hours (not joking - I give you permission to laugh)</p>