I am trying to connect to an oracle database in my controller:
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
// Other code
OracleConne
Add a try catch block around the open() call and resolve the ORA error that shows up in the OracleException.
I had the same problem when I started to use ODP.NET.
You can adjust your code like this:
try
{
OracleConnection con;
con = new OracleConnection();
con.ConnectionString = "DATA SOURCE=<DSOURCE_NAME>;PERSIST SECURITY INFO=True;USER ID=******;PASSWORD=*******";
con.Open();
}
catch (OracleException ex)
{
Console.WriteLine("Oracle Exception Message");
Console.WriteLine("Exception Message: " + ex.Message);
Console.WriteLine("Exception Source: " + ex.Source);
}
catch (Exception ex)
{
Console.WriteLine("Exception Message");
Console.WriteLine("Exception Message: " + ex.Message);
Console.WriteLine("Exception Source: " + ex.Source);
}
You can get more information about your error here: ORA-12154
The problem is your Data Source
in your connection string. I assume that it looks like this: Data Source=Server.Source
as you could find in your TNSNAMES.ORA
file on your computer.
The problem is that ODP.NET does not read the TNSNAMES.ORA
file as Visual Studio does.
You have multiple choices to solve this issue:
Web.Config
or App.config
file to tell ODP.NET how to handle your source.TNSNAMES.ORA
file in the same directory as the .exe
. (If it's not a web app)Data Source
in order to write the long version instead of the alias. E.g. : Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=sales-server)......)))
My favorite method is the #3. It's a lot easier to debug when you have a problem.
You can find more information in the dataSources Section of the Configuring Oracle Data Provider for .NET documentation.
I think there's an over-reaching try somewhere, or the stack makes me think this is an async controller call, but you're not using the async methods ("async all the way").
Explanation:
Something else is going on here. While I'm glad suggestions for a try/catch block helped you resolve the issue, they should not have been required with the simplified code bock - the ora exception should have been thrown in place of the null reference exception.
I had same problem and solved adding to my C# Project References
Oracle.ManagedDataAccess instead of Oracle.DataAccess.
To do this go to (Tools / Nugget Package Manager / Nugget Package Manager for Solution) browse for Oracle References and select Oracle.ManagedDataAccess.
Then you need to comment or delete your actual DataAccess calls to ManagedDataAccess as:
//using Oracle.DataAccess.Client;
//using Oracle.DataAccess.Types;
using Oracle.ManagedDataAccess.Client;
No need to change your Connection Code, only the using coding. After it works you can delete Oracle.DataAccess from your Project References
In my version the Oracle Path variable was set wrong ( cos there was another Oracle client installation before). So i suggest to take this possiblity into consideration too.
By following the My computer(this PC)->Properties->Advanced System Settings->Advanced->Enviromental Variables->Path you can edit Oracle Home path.