Devvox93
Devvox93

Reputation: 316

C# Web API connectionstrings web.config not being read

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:

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.

Upvotes: 0

Views: 6395

Answers (3)

Devvox93
Devvox93

Reputation: 316

Turns out it was a very simple fix, as is usually the case. As mentioned I have an app.config AND a web.config.

In the web.config are the Oracle Client settings, so because of that and prior experience I figured the connection string should be there too. When defining it in the app.config however, it works! Confusing with 2 config files though, as one would expect a connection string to be defined in a web.config, not app.config.

Bottom line: ConnectionStrings should be in the app.config.

That fixed it for me.

Upvotes: 0

Jared Lovin
Jared Lovin

Reputation: 551

Try the following. It is within System.Configuration:

var con = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString;

Also, you may want to try without the clear/> in your config.

Upvotes: 1

Mickers
Mickers

Reputation: 1449

I've only used an app.config file when I have a console application project in my solution for testing. I store my connection strings in the web.config like so:

<connectionStrings>
    <add name="NAME" connectionString="Server=Server;Initial Catalog=TABLE_NAME;User ID=ID;Password=PASSWORD;Application Name='WEBSITE'" />
</connectionStrings>

Then I reference it in my code like so:

protected const string ConnectionString = @"NAME";

Upvotes: 0

Related Questions