0

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, I'm sorry. That was what I originally tried and at the end of my question I mentioned that. Forgot to change it to the original in the code example. Bottom line: that didn't work for me.
1

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.

Comments

0

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";

3 Comments

Is it because you tell it it's a ConnectionString it knows to get it from the web.config? And then I could use it like below? new OracleConnection(ConnectionString);
No you can name it whatever you want. If setup properly in your web.config that name of your connection is available on the scope of your code. So I could call it 'protected const string SomeWhereOverTheRainbow = @"Name";' and that would work as well.
Turns out the web.config wasn't read, but the app.config instead. Don't know why, but the problem is solver :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.