title: Database Lookup From ComboBox selection
body: <p>I have a question about database values and how to determine the id of a value that has been changed by the user at some point. </p>
  
  <p>As it is currently set up there is a combobox that is populated from a dataset, and subsequent text boxes whose text should be determined by the value chosen from that combobox. </p>
  
  <p>So let's say for example you select 'Company A' from the combobox, I would like all the corresponding information from that company's row in the dataset to fill the textboxes (Name = Company A, Address = 123 ABC St., etc.,)</p>
  
  <p>I am able to populate the combobox just fine. It is only however when I change the index of the combobox that this specific error occurs:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Data.OleDb.OleDbException'
    occurred in System.Data.dll</p>
    
    <p>Additional information: Data type mismatch in criteria expression.</p>
  </blockquote>
  
  <p>Here is the corresponding code:</p>
  
  
  
  <pre class="lang-vb prettyprint-override"><code>Imports System.Data.OleDb
  Public Class CustomerContact
  
      Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|datadirectory|\CentralDatabase.accdb;")
      Dim da As New OleDbDataAdapter()
      Dim dt As New DataTable()
  
      Private Sub CustomerContact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          cn.Open()
          da.SelectCommand = New OleDbCommand("select * from Customers", cn)
          da.Fill(dt)
  
          Dim r As DataRow
          For Each r In dt.Rows
              cboVendorName.Items.Add(r("Name").ToString)
              cboVendorName.ValueMember = "ID"
          Next
          cn.Close()
      End Sub
  
      Private Sub cboVendorName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboVendorName.SelectedIndexChanged
          cn.Open()
          da.SelectCommand = New OleDbCommand("select * from Customers WHERE id='" &amp; cboVendorName.SelectedValue &amp; "'", cn)
          da.Fill(dt)
              Dim r As DataRow
              For Each r In dt.Rows
              txtNewName.Text = "Name"
              txtAddress.Text = "Address"
              Next
              cn.Close()
      End Sub
  </code></pre>
  
  <p>The error is caught at Line 24 of this code, at the second da.Fill(dt) . Now obviously from the exception I know that I am sending in a wrong datatype into the OleDbCommand, unfortunately I am a novice when it comes to SQL commands such as this. Also please keep in mind that I can't even test the second For loop, the one that is supposed to fill the Customer information into textboxes (for convenience I only copied the first two textboxes, of which there are nine in total). I am think I could use an If statement to determine if the row has been read, and from there populate the textboxes, but I will jump that hurdle when I can reach it.</p>
  
  <p>Any guidance or suggestions would be much appreciated. Again I am a novice at managing a database and the code in question pertains to the project my current internship is having me write for them.</p>