Link to home
Start Free TrialLog in
Avatar of webressurs
webressursFlag for Norway

asked on

Connectionstring App.config / App.Settings

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

ASKER CERTIFIED SOLUTION
Avatar of HarryNS
HarryNS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial