C# – Could not load file or assembly ‘Oracle.DataAccess’

asp.netcsharepoint

I have a Windows Server 2008 x64 machine and have successfully connected a simple ASP.NET/C# application to an Oracle database using the ODAC 11g client. My next step, is adding this same oracle connection to a SharePoint web part (which is developed on the same machine). The SharePoint Server, Web Server, etc. all live on the same machine as well. I figured I could just add the Oracle.DataAccess reference to my SharePoint Web Part, add the oracle connection code, and be done… but ohh now… I am getting the following error when loading my web part through the browser with the oracle connection:

[BadImageFormatException: Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.1, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

Can someone please help me debug this? I have the assembly in the GAC with the correct version. For some reason it is not seeing it? I am confused as to why a simple ASP.NET/C# application would work fine, but now that it is up on SharePoint it does not work. Any ideas?

SPECFIC ERROR:

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at Microsoft.SharePoint.WebPartPages.SPWebPartReflectionHelper.GetDefaultControl(Type controlType)
at Microsoft.SharePoint.WebPartPages.BinaryWebPartDeserializer.LoadInitialWebPart()
at Microsoft.SharePoint.WebPartPages.BinaryWebPartDeserializer.Deserialize()
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)

[BadImageFormatException: Could not load file or assembly 'Oracle.DataAccess, Version=2.112.1.1, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
at PortalOBIEEReportList.OBIEEReportList.OBIEEReportList..ctor()

Best Answer

You may need to enter a Safe Control setting for the oracle Data Access dll in the Web.Config file of your SharePoint application. The Safe Control setting explicitly identifies DLLs that the SharePoint runtime allows to be loaded.

The format is

<SafeControl Assembly="DLLName, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    Namespace="DLLNameSpace" 
    TypeName="*" 
    Safe="True" 
    AllowRemoteDesigner="True" />

Information on adding a SafeControl.

A tool I use to help determine the Assembly name is .NET Reflector