reading web.config from class library

我只是一个虾纸丫 提交于 2019-12-01 21:19:35

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"]

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>

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

appSettings is should be like

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!