Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I am developing a web application and i am using a class library with it, the problem i am facing is that how can i pass connection from my aspx.cs to class library classes, some techniques i am using but dont think that are better, that techniques are:

i am giving passing connection string to each class constructor and opening and closing it for any query, but i think this approach is not good, can any one please help me in this matter, i m really thankful to you people.


1 solution

  • You should have the ConnectionString in your web.config file of your web project.
  • Class library you are referencing from the Web project should read from the Web.config file.

Hope it helps :)
Share this answer
Zeeshan-Ali89 27-Apr-12 23:18pm    
but ConfigurationManager is not working in class library can u plz give me suggestion how to refer to web.config and is this method is good?
Mohammad A Rahman 27-Apr-12 23:51pm    
you need to add System.Configuration dll as reference to you class library and you could use this following method to grab the Conncetionstring by passing the connection string name,
namespace Utilities
using System.Configuration;

public static class ConfigurationReader
#region Public Methods

/// Gets the key value from configuration file.

/// <param name="key">The key.</param>
/// <returns>
public static string GetKeyValue(string key)
return ConfigurationManager.AppSettings[key];


Zeeshan-Ali89 27-Apr-12 23:53pm    
thxn a lot i will try it i think i was missing to add dll
Mohammad A Rahman 28-Apr-12 0:03am    
Not a problem :)
VJ Reddy 28-Apr-12 0:28am    
Good answer. 5!

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