This is my app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="radicalGuard"
         value="Data Source = MICHAEL-PC\SQLEXPRESS; Initial Catalog = radicalGuardDB; Integrated Security = True" />
  </appSettings>
</configuration>

And this is my connection string

SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["radicalGuard"].ToString());

Whenever I try to debug, it says "object reference not set to an instance of an object", and if I try to run without debugging it will say "application has stopped working"..

Need help.

Recommended Answers

All 4 Replies

You are using an obsolete deprecated method to retrieve the application settings.
I would prefer to use

SqlConnection con = new SqlConnection(System.Properties.Settings.Default.radicalGuard);

Hope this helps

commented: thanks +1

What's the difference other than its deprecated?

The difference, in the tests i did, is that the ConfigurationSettings.AppSettings is always returning an empty array (??), so it is currently compiling without error, but not working to me (VS2010 .NET 4.0).

Hope this helps.

I'm trying to change my config file to work with the new method of using it. I got an error saying appSettings has invalid child element settings.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <setting name="radicalGuard">
      <value>
        Data Source=.\SQLEXPRESS;Initial Catalog=radicalGuardDB;Integrated Security=True
      </value>
    </setting>
 </appSettings>
</configuration>

Where would I put the appsettings? Or am I missing something?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.