Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello,
I have a class library in Visual studio solution, i have added app.confing file in
class library
<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 29-Dec-11 0:32am
Edited 29-Dec-11 0:53am
Mehdi Gholam253.6K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why don't you pass your app's Settings object to the class in the class library?
  Permalink  
Comments
Mika Wendelius at 29-Dec-11 6:03am
   
Simple and feasible, 5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Comments
niravsahayata at 29-Dec-11 12:10pm
   
here is the solution i am using...http://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

try a configurationSetting
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  Permalink  
v3
Comments
niravsahayata at 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 at 30-Dec-11 9:10am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try a configurationSetting
 
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
  Permalink  
Comments
niravsahayata at 29-Dec-11 6:30am
   
this will not work for class library(i have already describes this in question)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Comments
rohit24c at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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.
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 265
1 Sergey Alexandrovich Kryukov 202
2 George Jonsson 185
3 King Fisher 185
4 CPallini 155
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 27 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100