reading web.config from class library

i have two project
1) class library with no inteface just an api
2) web application

from web apps i will be calling the class library api

so i have all the web.config settings in the web application but when i debug it always return me null value and here is the code snippit:

 public static string readingDB
        {
            get
            {
                string result = null;
                result = ConfigurationManager.AppSettings["employeeDB"]; //conn string
                if (!string.IsNullOrEmpty(result))
                {
                    return result;
                }
                else
                {
                    return "";  //???? THROW EXCEPTION???
                }
            }
        }

i have also tried, creating a new app.config in the class library project and have the same appsettings there but does not work…

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <applicationSettings>
    <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
  </applicationSettings>
  <customErrors mode="On"/>
</configuration>

any help?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

your syntax is incorrect, it should be

<configuration>  
  <appSettings>  
    <add key="employeeDB" value="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/> 
  </appSettings>  
</configuration>

or more correctly, since it’s a connection string,

<configuration>  
  <connectionStrings>  
    <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>  
  </connectionStrings>  
</configuration>

which would be read by ConfigurationManager.ConnectionStrings["employeeDB"]

Method 2

Also, the System.Configuration assembly is not automatically added to a class library.

  1. Right click on the project in the Solution Explorer
  2. Click Add > Reference
  3. Check System.Configuration in the Assemblies > Framework tab

Method 3

just saw the post and i had same problem but i got a way..
add System.Web.Configuration reference to your class library prj
then

ConnectingString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

Hope this will help

Method 4

Your tag is wrong..it should be ‘appSettings’ not ‘applicationSettings’

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add name="employeeDB" connectionString="Data Source=servername;Initial Catalog=employee;Persist Security Info=True;User ID=userid;Password=password;"/>
  </appSettings>
  <customErrors mode="On"/>
</configuration>

Method 5

appSettings is should be like

<appSettings>
    <add key="employeeDB" value="xxxx" />
</appSettings>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x