Exception: The key 'ConnectString' does not exist in the appSettings configuration section.

I am getting an exception stating: "The key 'ConnectString' does not exist in the appSettings configuration section." The app.config file contains the following:

<?xml version="1.0" encoding="utf-8"?>
    <add key="ConnectString" value="Server=HP-LAPTOP; Database=Test; User ID=xxxx; Password=xxxx; Trusted_Connection=false;"/>

The code throwing the exception is:

  ConnectString = (string)ConfigFile.GetValue("ConnectString", ConnectString.GetType());

I have other projects in the same solution where this is working.
Any suggestions?
Who is Participating?
ImperdonatoConnect With a Mentor Commented:
Use the following code to access your key:

using System.Configuration;

ConnectString = ConfigurationManager.AppSettings["ConnectString"];

You might have to add reference to System.Configuration, if it is not there.

Hope that helps.
You are using a class called ConfigFile to read the app.settings file. Make sure that class is included in your project.
tjlmAuthor Commented:

I tried your suggestion of using ConnectString = ConfigurationManager.AppSettings["ConnectString"];
Unfortunately it is returning null.
I have the using System.Configuration; but also added it as a reference and it made no difference.


The code includes AppSettingsReader ConfigFile = new AppSettingsReader(); which is in the .Net assembly System.Configuration. If I did not include this the project would not compile.

Thanks for the comments
tjlmAuthor Commented:
The problem appears to have gone away and I do not know why. I have been making additional changes but nothing that should have affected this item. Perhaps it was some glitch in VS whiich resolved itself. An error was occuring when there did not appear to be anything wrong. As well, other projects in the solution using the same code were working. Who knows???

I award the points to Imperdonato as he proposed a viable alternative.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.