Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,
I have a class library in Visual studio solution, i have added app.confing file in
class library
XML
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=ServerName; Database=DbNAme; UID=uid; password=password"/>
  </appSettings>
</configuration>

now i want to access ConnectionString from class library, how can i access it?
(this doesnt work for class library - ConfigurationManager.AppSettings["ConnectionString"];)
Posted
Updated 14-Nov-17 2:14am
v2

Why don't you pass your app's Settings object to the class in the class library?
 
Share this answer
 
Comments
Wendelius 29-Dec-11 6:03am    
Simple and feasible, 5.
I would guess it's not accessible from your class library as the class library doesn't reference the System.configuration component. Make sure this is referenced by your class library.

Also, depending on the version of .Net you are using, you can add a connection string directly to the connectionStrings element of your config file and access it using ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString rather than the obsolete ConfigurationSettings.AppSettings method.
 
Share this answer
 
v2
Comments
niravsahayata 29-Dec-11 12:10pm    
here is the solution i am using...http://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project
try a configurationSetting
C#
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
 
Share this answer
 
v3
Comments
niravsahayata 29-Dec-11 12:09pm    
i am using same syntax now..,but using this syntax i can access config file of solution, i cant access confing file for class library.
follow this link..http://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project
Espen Harlinn 30-Dec-11 9:10am    
5'ed!
try a configurationSetting

C#
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
 
Share this answer
 
Comments
niravsahayata 29-Dec-11 6:30am    
this will not work for class library(i have already describes this in question)
 
Share this answer
 
Comments
rohit24c 21-Aug-13 5:56am    
If u add a Key/Value pair in AppConfig and if its a WebApp then donot forget to add it in webConfig file.
As the webconfig overrides all the value in appconfig.
It will work but it has to be in the config file for whatever is hosting your dll. For example, if your dll is used in a asp.net site you would need to add it to the site's web.config.
 
Share this answer
 
all you nee is to select the app.config file and change the follow properties :
Build Action -> Embedded Resource
Copy to output Directory -> Copy if newer
 
Share this answer
 
Comments
CHill60 14-Nov-17 15:10pm    
Question was asked and answered 6 years ago!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900