This is my first question here, so please be gentle (and provide constructive criticism as to how I can improve my question) :)
I'm trying to connect to an Oracle Database in my Web API project. This project has an app.config and a web.config. I don't know why I have both, but it came with that when setting up the (empty asp.net with Web API) project.
This is my code to connect to the database and get the connection string, which is also where I get the NullReferenceException:
// create a connection to the database
using (var con = new OracleConnection(ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString))
And I have included the appropriate using-statements:
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Data;
using Oracle.ManagedDataAccess.Types;
My web.config file is as follows:
<configuration>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<!-- Customize these connection alias settings to connect to Oracle DB -->
<dataSource alias="ALIAS" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=IPOFSERVER)(PORT=PORTOFSERVER))(CONNECT_DATA=(SERVICE_NAME=SERVICENAME)));User Id=USERNAME;Password=PASSWORD;" />
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<clear/>
<add name="Oracle" connectionString="Data Source=ALIAS;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>
</configuration>
Things I've tried so far:
- Using System.Web.Configuration.WebConfigurationManager instead
- Checking references etc.
- Adding AppSettings key and value and try to read that from the web.config file. Also returns null.
I'm not getting any errors other than the runtime NullReferenceException. I hope this is enough information to suggest a solution. If not, please let me know and I'll provide extra info.