Question

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?

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top