title: Read data from a database in vb.net
body: <p>So I used this piece of sample code to retrieve data from a MS Access Database and display it on a few textboxes on the form. The following error occurs - </p>
  
  <blockquote>
    <p>@ dr = cmd.ExecuteReader - Data type mismatch in criteria expression.
    dr = cmd.ExecuteReader</p>
  </blockquote>
  
  <p>This is the sample code - </p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim provider As String
  Dim dataFile As String
  Dim connString As String
  Public myConnection As OleDbConnection = New OleDbConnection
  Public dr As OleDbDataReader
  
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
      dataFile = "C:\Users\example\Desktop\Data.accdb" ' Change it to your Access Database location
      connString = provider &amp; dataFile
      myConnection.ConnectionString = connString
  End Sub
  
  Dim r As Random = New Random
  
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      myConnection.Open()
      TextBox1.Clear()
      TextBox2.Clear()
      TextBox3.Clear()
      Dim str As String
      str = "SELECT * FROM Items WHERE (Code = '" &amp; r.Next(1, 3) &amp; "')"
      Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
      dr = cmd.ExecuteReader
      While dr.Read()
          TextBox1.Text = dr("Description").ToString
          TextBox2.Text = dr("Cost").ToString
          TextBox3.Text = dr("Price").ToString
      End While
      myConnection.Close()
  End Sub
  </code></pre>