Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Connectionstring  App.config / App.Settings

Posted on 2009-05-14
2
Medium Priority
?
2,715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
HarryNS earned 1000 total points
ID: 24384756
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 1000 total points
ID: 24558275
Use this:

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

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 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