10

I have a config file in a wpf project to store the connectionstring. But when I try to get AppSettings and ConnectionStrings, I get null.

the WEB.config file is like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="Trackboard" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </connectionStrings>
  <appSettings>
    <add key="Trackboard" value="Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf"/>
  </appSettings>
</configuration>

I tried in several ways:

W1: ConnStr = ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
W2: ConnStr = ConfigurationManager.ConnectionStrings[0].ConnectionString;
W3: ConnStr = ConfigurationManager.AppSettings["Trackboard"];
W4: ConnStr = ConfigurationManager.AppSettings[0];

None of them worked.

But this one worked:

ConnStr = @"Data Source=(localdb)\v11.0;Initial Catalog=Trackboard;Integrated Security=True;AttachDbFileName=E:\Users\Sean\Workspace\DATABASE\Trackboard.mdf";

(That means I cannot use a config file, which is against my will) I need help.

2
  • 2
    Why you choose WEB.config for WPF app. It have to the APP.config. Commented Feb 7, 2014 at 7:47
  • :P I just renamed it to WEB.config Commented Feb 7, 2014 at 14:08

3 Answers 3

15

Just add an app.config and not web.config because it is not a web application.

And after that it's too simple, just add a reference to to System.Configuration and then use this.

var ConnStr = ConfigurationManager.AppSettings["Trackboard"];
Sign up to request clarification or add additional context in comments.

Comments

2

This one use System.Configuration namespace

using System.Configuration;


Or add System.Configuration in reference

System.ConfigurationManager.ConnectionStrings["Trackboard"].ConnectionString;
System.ConfigurationManager.ConnectionStrings[0].ConnectionString;

1 Comment

I did add the System.Configuration reference and namespace. The problem seems to be that System.ConfigurationManager.ConnectionStrings has a count of 0, that's why I can't get the value by key or index.It's weird.It works in winform application.
2

I've figured it out! I shouldn't have created a new config file. There is a default app.config file in the project. Now everything is fine.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Trackboard.Properties.Settings.TrackboardConnectionString"
            connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DATABASE\Trackboard.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
private static string ConnStr = ConfigurationManager.ConnectionStrings["Trackboard.Properties.Settings.TrackboardConnectionString"].ConnectionString;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.