I created a C# dll that uses connection string from app.config file to connect to database.
I use it in a web application (Visual Studio 2013, Windows 7) that also connects to a database and has its own connection string in web.config file.
However, when web app uses dll, the dll cannot find connection string that it needs from app.config.
I realized that dll was looking for the connection string in web.config. For now, I added that connection string to web.config and it is working fine.
My question is:
Is it the correct way of doing it? Or, there is a way for the dll to find its app.config and load connection string from there?
Actually the app.config file in your dll (class library project) won't affect the final host application ( in which the dll is used). So you will always need to put the proper connection settings in the host application's config file. For example, if you use the assembly/dll in winform or WPF application, then you should put the related configuration (like connection strings) in the winform or WPF app's app.config file. If the assembly/dll is used in ASP.NET web application, then we should make sure the related settings are put in the web.config file of the web application.
Hardcoding the database connection in your referenced DLL's app.config file may not be the best approach. Setting the database connection string in the web.config file (or machine.config for a machine-wide setting) would allow other consumers of the DLL to use a different connection string. This would be useful if, for example, you have different Development, Quality Assurance, or User Acceptance environments with different databases that each of those deployed web applications would need.
However, if that approach does not fit your needs, using Application Settings you can use and/or override default values from another referenced DLL. For example, if you have an assembly called Foo and within that, you used setting properties, you'd have a section of your Foo's app.config file that had something like this:
<applicationSettings>
<Foo.Properties.Settings>
<setting name="MyProperty" serializeAs="String">
<value>MyValue</value>
</setting>
</Foo.Properties.Settings>
</applicationSettings>
Then, within your web application, you can use this property by calling Foo.Properties.Settings.MyProperty. This approach would also allow you to set values that the Foo assembly could use. Within your web.config file, you'd include the section...
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Foo.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
... then add the Foo.Properties.Settings section, updating it with the values you want the Foo.DLL to use when it is included as part of your web application.
This approach would allow you to set values that the application would use when executing code within the referenced DLL. In your case, you'd just need to put the connection string into an application setting rather than including it within the connection strings section.