title: VB.NET Access - Attempted to read or write protected memory
body: <p>I have a problem where my code is generating this error:</p>
  
  <blockquote>
    <p>System.AccessViolationException: 'Attempted to read or write protected
    memory. This is often an indication that other memory is corrupt.</p>
  </blockquote>
  
  <p>This occurs at the line <code>My_CONNECTION.Open()</code>.</p>
  
  <p>Any idea how to fix this?</p>
  
  <p>My code:</p>
  
  <p><strong>vb.net</strong></p>
  
  <pre><code>Dim My_USER As String = Environment.UserName
  Dim My_CONNECTION As New OleDbConnection
  Dim My_DATABASE As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\" &amp; My_USER &amp; "\AppData\Roaming\MYAPP\MYDTB.accdb;Persist Security Info=False;"
  Dim My_COMMAND As New OleDbCommand
  Dim My_READER As OleDbDataReader
  Dim My_COUNT As Integer
  
  '### Open connection ###
  
  My_CONNECTION.ConnectionString = My_DATABASE
  My_CONNECTION.Open()
  
  '### SQL ###
  
  With My_COMMAND
  .CommandText = "SELECT COUNT(*) FROM MYDTB"
  .CommandType = CommandType.Text
  .Connection = My_CONNECTION
  End With
  
  My_COUNT = My_COMMAND.ExecuteScalar()
  
  '### Close connection ###
  
  My_CONNECTION.Close()
  </code></pre>