Solved

Connectionstring  App.config / App.Settings

Posted on 2009-05-14
2
2,693 Views
Last Modified: 2012-05-07
I have a strage problem. When trying to read the connectionstring from App.config (System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString) I get Null Reference Exception even if the connection string is correct written in the App.config file. I have also set correct reference to the System.configuration.dll. When I put the connectionsting in a App.settings file it works without problems. I have tried everything and is going nuts. What can cause this problem?

Please see attached code :)
-----------------------------

App.config

-----------------------------
 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

	<configSections></configSections>

	<connectionStrings>

  <add name="ConnectionString" connectionString="Data Source=localhost;Initial Catalog=Rank;Persist Security Info=True;User ID=Rank;Password=Password;"

   providerName="System.Data.SqlClient" />

  <add name="Rank.App.ConnectionString" connectionString="Data Source=localhost;Initial Catalog=Rank;Persist Security Info=True;User ID=Rank;Password=Password;" />

 </connectionStrings>

</configuration>
 

namespace Rank

{

    public class Common

    {

        public class Data

        {

            public static string ConnectionString

            {

                get

                {

                    //THIS DONT WORK:

                    //return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 

                    //THIS WORK PERFECT:

                    return App.Default.ConnectionString.ToString();

                }

            }

        }

    }

}

Open in new window

0
Comment
Question by:webressurs
2 Comments
 
LVL 6

Accepted Solution

by:
HarryNS earned 250 total points
Comment Utility
MessageBox.Show(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

With the app.config you mentioned, when I tried here I am able to retrieve the following result,

Data Source=localhost;Initial Catalog=Rank;Persist Security Info=True;User ID=Rank;Password=Password;
0
 
LVL 7

Assisted Solution

by:csharpp
csharpp earned 250 total points
Comment Utility
Use this:

return System.Configuration.ConfigurationManager.ConnectionStrings["Rank.App.ConnectionString"].ConnectionString;

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

10 Experts available now in Live!

Get 1:1 Help Now