3

I deployed my asp.net web app project that has a reference to my DAL class library. How can I change the connection string in my DAL library once deployed? The DAL code was ignoring my web.config connection string and trying to use the app.config value.

I thought I would be able to edit a config file that is associated with the class library, but I can't find one. Temporarily I edited the connection string and re-compiled and re-deployed the library.

Is there an option or way to setup the project files where it changes the values of the connection string based on being compiled in debug mode versus doing a release compile. What is the recommended way of dealing with connection strings in web apps which reference class libraries?

Clarification: the DAL library connection strings are also utilized by some datasets and L2S classes (.dbml) and I am not sure how to change those to reference a web.config file that sits outside the library in the web app project.

Currently Using this code to get around my L2S class problem:

public partial class MyDataContext 
{
    partial void OnCreated()
    {

        ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];

        if (cs != null)
        {
            this.Connection.ConnectionString = cs.ConnectionString;
        }
    }
}

3 Answers 3

2

Generally, I let the top-level project define this, via either web.config or app.config; either by specifying that the application should include a connection-string named "FOO", or (much better) allowing the calling application to pass (one of) the connection key, the connection string, or the connection to the dll.

Then you mainly just edit web.config like normal...

1
  • I had originally done that, but thought somehow it might be safer having it in the class library from an isolation perspective so I moved it into the library project settings.
    – Breadtruck
    Jul 18, 2009 at 22:23
2

I usually place the string in the web.config file and let a class (possibly a global class used to reference settings in the web.config) reference it, with the following line:

class Globals
{
    static string ConnectionString =
        ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
}

and use it elsewhere like:

IApartmentRepository apartmentRepository = new
    ApartmentRepository(Globals.ConnectionString);

This was also Saif Khan's suggestion in his answer.

As for changing the connection strings based on compile mode, Visual Studio 2010 has support for this, see Web Deployment: Web.Config Transformation from the Visual Web Developer team blog. I do not think Visual Studio 2008 can do this out of the box, however maybe it can be done with some kind of build script.

1
  • Do you need to add a reference in the class library to the web project to get at the web.config?
    – Breadtruck
    Jul 18, 2009 at 22:29
0

I don't believe there is a recommended way, but a prefered way. Some store connection strings in the web.config file, while some store in the registry or machine.config, while some go to the extreme and store it remotely...yes I've seen that.

The most common storage I see and use myself is storing in the web.config. In my DAL objects I make a call to the web.config file for the connectionstring

string connStr = ConfigurationManager.ConnectionStrings["myString"].ConnectionString

as for auto changing of the connectionstring based on the app compiled mode, I've never seen that. You might have to put that check in the DAL itself to check if debug mode is turned "on" or "off". That would require 2 connectionstring entries in the web.config file.

2
  • Please see my clarification in my main question. How would I reference the web.config file from the class library, without actually passing it in as Marc Gravell suggested?
    – Breadtruck
    Jul 18, 2009 at 22:31
  • import the system.configuration namespace.
    – Saif Khan
    Jul 19, 2009 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.