I have written connection in web.confic file in
appSettings.
But when i want to use that connection in ClassLibrary(DataAccessLayer), it do not shows
ConfigurationManager.AppSettings whether i have added the namespace
System.Configuration.
This problem comes only in ClassLibrary but in .aspx.cs page i can access the same connection string successfully with
ConfigurationManager.AppSettings.
Anyone tell me the reason, why i can not access the connection string in ClassLibrary with
ConfigurationManager.AppSettings .
Thanks
Pradeep Kr. Sharma
Pradeep Kr. Sharma
http://vshelpdesk.blogspot.com/
Please Mark As Answer, which helped you. So that it might be useful for others.
A class library shouldn't really be directly referencing anything in app.config - the class doesn't have an app.config, because it's not an application, it's a class.
Anything that needs to be passed to the class from the application, should be just that, passed to the class. For instance, if you have a connection string in your app.config, this should be passed as a parameter to your data access layer.
The fact that you can use this workaround (in the same way that you can access the HTTPContext from a class) doesn't mean that it's good practice to do so.
Try adding it as a parameter, and you'll see instantly how much nicer the code is to write AND (more importantly) to maintain.
Pradeep Kr. ...
Participant
1712 Points
294 Posts
Unable to use ConfigurationManager.AppSettings in ClassLibrary
Jan 15, 2009 04:19 PM|LINK
Hi All,
I have written connection in web.confic file in appSettings.
But when i want to use that connection in ClassLibrary(DataAccessLayer), it do not shows ConfigurationManager.AppSettings whether i have added the namespace System.Configuration.
This problem comes only in ClassLibrary but in .aspx.cs page i can access the same connection string successfully with ConfigurationManager.AppSettings.
Anyone tell me the reason, why i can not access the connection string in ClassLibrary with ConfigurationManager.AppSettings .
Thanks
Pradeep Kr. Sharma
http://vshelpdesk.blogspot.com/
Please Mark As Answer, which helped you. So that it might be useful for others.
Curt_C
All-Star
66014 Points
7639 Posts
Moderator
Re: Unable to use ConfigurationManager.AppSettings in ClassLibrary
Jan 15, 2009 05:08 PM|LINK
is your DAL a seperate project? Are you referencing the System.Web dll?
v5.1 of iTracker (Inventory Tracker Starter Kit) is out, Download it now!
Pradeep Kr. ...
Participant
1712 Points
294 Posts
Re: Unable to use ConfigurationManager.AppSettings in ClassLibrary
Jan 15, 2009 06:31 PM|LINK
Yes, I have added class library as a new project. yes i referencing the System.Web dll?
But i have not find the solution.
Regards
Pradeep Kr. Sharma
http://vshelpdesk.blogspot.com/
Please Mark As Answer, which helped you. So that it might be useful for others.
sirdneo
All-Star
15171 Points
2509 Posts
Re: Unable to use ConfigurationManager.AppSettings in ClassLibrary
Jan 16, 2009 09:37 AM|LINK
1- First you need to add refrence to System.Configuration.dll refrence to your class library.
2- add using System.Configuration; in using block.
3- then you can access it like ConfigurationManager.AppSettings["SettingName"]
Zeeshan Umar
~Please Mark As Answer, one or multiple posts, which helped you. So that it might be useful for others~
Mr^B
Star
12726 Points
2245 Posts
Re: Unable to use ConfigurationManager.AppSettings in ClassLibrary
Jan 16, 2009 10:28 AM|LINK
A class library shouldn't really be directly referencing anything in app.config - the class doesn't have an app.config, because it's not an application, it's a class.
Anything that needs to be passed to the class from the application, should be just that, passed to the class. For instance, if you have a connection string in your app.config, this should be passed as a parameter to your data access layer.
The fact that you can use this workaround (in the same way that you can access the HTTPContext from a class) doesn't mean that it's good practice to do so.
Try adding it as a parameter, and you'll see instantly how much nicer the code is to write AND (more importantly) to maintain.