How to make connection strings identical

A

ad

I have a web application which refer a class library project.
The web application have a web.config and the class library project have a
app.config.
They all have connection string defined in them.

I set both connection strings identical in VS2005.

But after publish it to web site, I can just set the connection in the web
application.

How can I set the app.config in the class library after publish?
 
E

Eliyahu Goldin

A class library shouldn't care whether configuration parameters reside in a
web.config or an app.config file. What file is used depends on the platform.
For Windows it will be app.config, for web - web.config. You can't use both.

Eliyahu
 
O

Otis Mukinfus

I have a web application which refer a class library project.
The web application have a web.config and the class library project have a
app.config.
They all have connection string defined in them.

I set both connection strings identical in VS2005.

But after publish it to web site, I can just set the connection in the web
application.

How can I set the app.config in the class library after publish?
In the class library, make an overloaded constructor that takes a connection
string as a parameter:

/// <summary>
/// Data access methods for the USCities table
/// </summary>
public class USCityTable
{
private string _connectionString;

#region ctors

/// <summary>
/// Initialization ctor
/// </summary>
/// <param name="connectionString">Database connection string</param>
public USCityTable(string connectionString)
{
_connectionString = connectionString;
}

#endregion ctors

#region public methods

/// <summary>
/// Select all cities in the USCities table
/// </summary>
public USCities SelectAllCities()
{
SqlConnection cn = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand("sp_FetchAllCities", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = null;
USCities cities = new USCities();


After retrieving the connection string from the web.config file set the
connection string for the class like this:

USCityTable citytable = new USCityTable("yourConnectionString");


Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top