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

Posted on 2009-02-13
Last Modified: 2013-12-16
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?
Question by:tjlm
    LVL 5

    Accepted Solution

    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.
    LVL 2

    Expert Comment

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

    Author Comment


    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

    Author Closing Comment

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now