Click here to Skip to main content
11,644,982 members (65,676 online)
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 28-Dec-11 23:32pm
Edited 28-Dec-11 23:53pm
Mehdi Gholam282.9K
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 OriginalGriff 725
1 jyo.net 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 404
4 CPallini 386
0 OriginalGriff 1,020
1 Sergey Alexandrovich Kryukov 673
2 DamithSL 656
3 Afzaal Ahmad Zeeshan 652
4 CPallini 555


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 27 Sep 2013
Copyright © CodeProject, 1999-2015
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