VB.NET issue connecting to Oracle 11g

odp.netoracle11gvb.netvb.net-2010

OS: Windows 7 64bit
VB: Visual Studio 2010
Oracle Client: 11g

I am developing an application that connects to an Oracle 11g server. I am able to connect to the Oracle server via Oracle SQL Developer, ODBC (in SYSWOW64), and in VB Server Explorer.

I am using the .NET reference Oracle.DataAccess located at: C:\Oracle\product\11.2.0\client_32\ODP.NET\bin\2.x\Oracle.DataAccess.dll

I have tried adding the .DLL files to my bin folder as suggested by other questions and that does not work.

Does anyone have any other suggestions?

When I run my application, I get the following:
enter image description here

Here are the details:

System.TypeInitializationException was unhandled
  Message=The type initializer for 'Oracle.DataAccess.Client.OracleConnection' threw an exception.
  Source=Oracle.DataAccess
  TypeName=Oracle.DataAccess.Client.OracleConnection
  StackTrace:
       at Oracle.DataAccess.Client.OracleConnection..ctor(String connectionString)
       at Orc_Test_1.Form1.Button1_Click(Object sender, EventArgs e) in c:\Visual Studio 2010\Projects\Orc_Test_1\Orc_Test_1\Form1.vb:line 9
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Orc_Test_1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Oracle.DataAccess.Client.OracleException
       DataSource=""
       ErrorCode=-2147467259
       Message=The provider is not compatible with the version of Oracle client
       Number=-11
       Procedure=""
       Source=Oracle Data Provider for .NET
       StackTrace:
            at Oracle.DataAccess.Client.OracleInit.Initialize()
            at Oracle.DataAccess.Client.OracleConnection..cctor()
       InnerException: 

VB.NET Code:

Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim oradb As String = "Data Source=SERVER1;Persist Security Info=True;User ID=username;Password=password;Unicode=True" 'From Server Explorer
        'Dim oradb As String = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=Server1.host.net)(PORT=1522))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=Server1))); USER ID = username;Password = password;" 'From TNSnames.ora
        Dim conn As New OracleConnection(oradb)  '<--- Error happens on this line
        conn.Open()
        Dim cmd As New OracleCommand
        cmd.Connection = conn
        cmd.CommandText = "select system_id from schema1.system_impacted where system_name = AWESOME"
        cmd.CommandType = CommandType.Text
        Dim dr As OracleDataReader = cmd.ExecuteReader()
        dr.Read()
        Label1.Text = dr.Item("system_id")
        conn.Dispose()
    End Sub
End Class

Best Answer

I feel your pain, just went through something similar in a deployment situation. You probably have multiple clients installed, and your environment is pulling dlls for older releases (even if you have a latest oracle.dataaccess.dll correctly referenced in your project). Fixing this on your dev environment is one thing, a prod deployment server is another. Not sure what your deployment situation is, but here's what worked for me.

After struggling with trying to upgrade odp.net in existing oracle home, adding new oracle home, etc., I found the easiest way to fix everything is to download the latest odac with xcopy deployment from Oracle, and follow the readme (and see here for an older article on this also). Basically you'll run an install.bat file to setup locally (in separate folder, mine was c:\oracle_odac), then change your project reference to point to the oracle.dataaccess.dll in this new folder (I used 4 instead of 2.x), and add the new folder's bin dirs to front of your path (c:\oracle_odac\bin and c:\oracle_odac\odp.net\bin\4). On your deployment server, you'll just need to copy the entire c:\oracle_odac folder over (via xcopy or however), and setup the path.

That said, I anxiously await the production release of the fully managed odp.net from Oracle (in beta now).

EDIT: Just to add that you can avoid messing with PATHs if you setup in your app or web config file the dllpath. For example:

<configuration>
...
    <configSections>
      <section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>  
...

<oracle.dataaccess.client>
    <settings>
      <add name="DllPath" value="c:\oracle_odac\bin"/>
    </settings>
  </oracle.dataaccess.client>
...

This will override other settings such as registry or machine.config. And it will allow multiple odp.net configurations to exists peacefully, and allow each app to point to the version it needs on the same server.

Related Topic