0

I have created a settings class where I added some parameters that are shown in the app.config below.

I want to access the connectionStrings and the usersettings inside my class but my attempts are not successful.

I've tried for example :

ConfigurationManager.ConnectionStrings['Alerter.AlerterSettings.ConnectionString']

But it doesnt work.

Here is the app.config:

<connectionStrings>
    <add name="Alerter.AlerterSettings.ConnectionString"     connectionString="DefaultEndpointsProtocol=https;sometext" />
</connectionStrings>
<userSettings>
    <Alerter.AlerterSettings>
        <setting name="TableName" serializeAs="String">
            <value>table5</value>
        </setting>
        <setting name="EmailUser" serializeAs="String">
            <value>user</value>
        </setting>
        <setting name="EmailPass" serializeAs="String">
            <value>pass</value>
        </setting>
    </Alerter.AlerterSettings>
</userSettings>

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
Marwan Tushyeh
  • 1,505
  • 4
  • 24
  • 47
  • Do you have an _actual_ SQL connection string there? – Oded Jan 25 '13 at 20:36
  • @MUG4N - Why should they OP create a _custom_ configuration section? – Oded Jan 25 '13 at 20:41
  • @Oded: the connectionStrings section doesn't have to hold SQL connection strings. There's no validation on the values in there. People store SQL connection strings there by convention - the documentation for that section doesn't mention SQL or database anywhere except the example code. – Sam Axe Jan 25 '13 at 20:42

1 Answers1

3

Don't use ' - it is that character delimiter in C#, not a string delimiter:

ConfigurationManager.ConnectionStrings["Alerter.AlerterSettings.ConnectionString"]

You will also need to access the ConnectionString property of thee returned value:

string theConnectionString = 
            ConfigurationManager.ConnectionStrings["whatever"].ConnectionString;

Note on naming:

Alerter.AlerterSettings.ConnectionString is rather verbose - it is not very readable in my opinion, and this is not needed for a configuration option - in particular as it is in the connectionStrings section. AlerterSettings to my eyes is much more appropriate.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • but ConfigurationManager.ConnectionStrings["Alerter.AlerterSettings.ConnectionString"].ConnectionString causes a null reference exception – Marwan Tushyeh Jan 25 '13 at 20:37
  • 1
    @user1709794 - Are you certain you have that value in the config file? Exactly like that? That error suggests that you may have put it in the wrong place (a different `.config` file?). – Oded Jan 25 '13 at 20:39
  • yes it turns out it access the app.config of another project, how can i fix that? – Marwan Tushyeh Jan 25 '13 at 21:26
  • @user1709794 - Is this a class library? If so, that would be correct - it should access the configuration of the application that it using it. See: http://stackoverflow.com/questions/5674971/app-config-for-a-class-library – Oded Jan 25 '13 at 21:33
  • yes it is a class library, but the problem is that i am using it in two applications, so i was trying to put the settings inside the class library by creating a .settings class, the .settings class automatically created an app.config – Marwan Tushyeh Jan 25 '13 at 21:41
  • @user1709794 - I see. That's where they are stored, but you shouldn't - create `.settings` in both applications that use it - that should work better. – Oded Jan 25 '13 at 21:42