UPDATE: I would not recommend using this approach. You shouldn't change any code that is automatically generated because it might get overwritten when modifying the designer.
When you add a DataTable with a DataAdapter on a DataSet for a web app, Visual Studio takes you through a wizard to set some properties of the DataAdapter. One of those properties is the ConnectionString and it saves that info on the web.config, which is exactly where we want that piece of information. The problem comes when you want to set the DataSet on an external project with a DLL output. For example when you're implementing N-Tiering (the DataAccess layer.) The ConnectionString gets saved on the app.config file.
When you add the reference of the DataAccess layer the app.config doesn't get moved to the bin directory of the project and therefore the reference to the ConnectionString is lost. You could move that file manually and would solve the problem, but you wouldn't want to have the ConnectionStrings on multiple files, would you? It becomes a nightmare when you want to change them. Over here I'm going to show you a work around for this.
When you use the wizard, Visual Studio creates the code for you, that code is stored on the [DataSetName].Designer.cs (or .vb). What we need to do is change the location of where the ConnectionString is retrieved from. In my case the DataSet is called Product so on my Product.Designer.cs I have this:
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void InitConnection() {
this._connection = new System.Data.SqlClient.SqlConnection();
this._connection.ConnectionString = global::DataAccess.Properties.Settings.Default.AdventureWorksConnectionString;
}
the second line contains the ConnectionString which is being accessed from the Settings file, which eventually retrieves the value from the App.Config. We need to comment that line out:
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void InitConnection() {
this._connection = new System.Data.SqlClient.SqlConnection();
//this._connection.ConnectionString = global::DataAccess.Properties.Settings.Default.AdventureWorksConnectionString;
}
and we need to add a new constructor with a string parameter, which will represent the ConnectionString:
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ProductTableAdapter(string ConnectionString)
{
this.ClearBeforeFill = true;
InitConnection();
this._connection.ConnectionString = ConnectionString;
}
Over here we do the same thing as the default constructor and at the end we set the ConnectionString property of our connection with the ConnectionString passed in to the constructor.
So from your web project, you would initialize the ProductTableAdapter like this:
private ProductTableAdapter pta = new ProductTableAdapter(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
This small changes will give you more flexibility on the storage of your ConnectionStrings.
Happy Programming!
0eb2e2f9-352f-4a6d-ae48-4a0e1d3c454f|0|.0|27604f05-86ad-47ef-9e05-950bb762570c