views:

500

answers:

1

The Setup:

I have a DLL that uses a ConnectionString to connect to a SQL database. When I add the DLL to my website i have to add the connectionstring into my web.config in order for my DLL to function properly (this is by design). Once I add it into the web.config then everything works fine (as expected).

The Problem:

The problem begins when I want to move the connectionstring into my Website's ASP.NET Application settings found in IIS > Default Website > Properties > ASP.NET tab > Edit Global Configuration... > Connection string manager

If I remove it from my web.config and put it there my DLL fails to work. If I use the connectionstring anywhere else in my website (and not from a DLL) I can access the database just fine through this method but for some reason my DLL can only access it if it's in the web.config.

The Question:

How can I get my DLL to use the connectionstring that's listed in the ASP.NET Configuration Settings Connection String Manager instead of the web.config?

+2  A: 

You have to look for the section from the general ASP.Net configuration settings, which can be retrieved through the WebConfigurationManager class, rather than looking for a connection string via ConfigurationManager.ConnectionStrings.

    // Get the connectionStrings section.
ConnectionStringsSection connectionStringsSection =
    WebConfigurationManager.GetSection("connectionStrings")
    as ConnectionStringsSection;

// Get the connectionStrings key,value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
    connectionStringsSection.ConnectionStrings;

// Get the collection enumerator.
IEnumerator connectionStringsEnum =
    connectionStrings.GetEnumerator();
jro
So, I do this inside my dll, correct?
EdenMachine
also, Linq to SQL overwrites the db connectionstrings every time you save the DBML so I always have to open in notepad to override it and then build it right afterwards.
EdenMachine
Yes, inside your DLL.
jro
Overwrites the db connection strings? Meaning, your local config file?
jro
No, I mean if changes are made to the designer.vb code for the DBML, visual studio will overwrite any manual changes when you save.
EdenMachine
Yes, that's true. That's a different issue from this.
jro