0

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.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by AngelicOne
1

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

Votes + Comments
thanks
0

What's the difference other than its deprecated?

Edited by AngelicOne: n/a

1

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.

0

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.