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>

3
  • Do you have an actual SQL connection string there? Commented Jan 25, 2013 at 20:36
  • @MUG4N - Why should they OP create a custom configuration section? Commented Jan 25, 2013 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. Commented Jan 25, 2013 at 20:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

but ConfigurationManager.ConnectionStrings["Alerter.AlerterSettings.ConnectionString"].ConnectionString causes a null reference exception
@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?).
yes it turns out it access the app.config of another project, how can i fix that?
@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: stackoverflow.com/questions/5674971/…
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
|

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.