locked
Place Connection String in WebConfig or AppConfig (but which project???) RRS feed

  • Question

  • User867713863 posted

    My application is working but I'm not sure why.  The problem is that I didn't have my stored procedure in SS2012.  So when I kept getting a null reference i thought it was because of that, but evidently not. Can someone tell me how this works. or which project has to have the config file for the connection string to work right.

    I have 3 projects.  this is an N-tier application.  I have a DataAccess tier as a class library. I have two web applications (one for testing only before applying WCF (called TestHarness) and one that will serve as the Presentation tier through the WCF). 

    Scenario 1:  Originally I had the connection string in my web config file of what would become my Presentation tier. I referenced my presentation tier in my DataAccess tier. It appeared that after doing this all my errors disappeared. (Perhaps this was illusory.) However, i was calling my test application as the start up page. (My thinking was that my DataAccess would get the connection string from the reference in the Presentation tier and once that was accomplished my DA methods would have all that was needed to execute in my "TestHarness.")  I didn't see why my so-called "TestHarness" needed a duplicated connection string.

    Scenario 2:  I added both a connection string to my TestHarness and an appConfig to my DataAccess class library. Everyting worked.  (I still assumed it was because of the AppConfig attached to the DA tier rather than the WebConfig of the TestHarness.)

    Scenario 3:  In the TestHarness, I changed the name of the connection string and made sure it did not work.  The appConfig of the DataAccess tier was still available. It seemed to make a lot of sense that I could pull the connectionstring from the AppConfig included in my DataAccess class library to make the connection. Then the DA could be stand alone. No matter what project i added later, i thought, it would not need its own connection string it is web config.  this makes a lot of sense to me but evidently, it doesn't work that way. I got a null reference error.

    Conclusion: My conclusion is that each project needs a connection string in its web or app config file. Somehow the DA tier will find this reference and make the connection.

    can someone please explain or direct me to a link that will. thanks!  Since I'm not passing the connection string as a parameter, this just doesn't make sense to me.  I can only assume that the DA is getting what i needs from the reference these projects are making to it.

    Thanks!

    Friday, April 19, 2013 5:30 PM

Answers

  • User1124521738 posted

    if the class is referenced by your webgui and the assembly is copied into the webproject's bin directory, the web.config in the web project will need the connection string copied from the class library's app.config.  if the class library is used by some service (like a web service) that lives elsewhere, then the connection string will need to go with that other project.  Having the app.config in the class library can be a bit misleading, but since class libraries are never run on their own (something else always calls them) it's the consuming projects that need the connection string.  Keeping a copy of the app.config with the class provides a source of truth for consumers that need to add the library so the developer will know what to copy to their project's app or web.config.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 19, 2013 5:45 PM

All replies

  • User1124521738 posted

    if the class is referenced by your webgui and the assembly is copied into the webproject's bin directory, the web.config in the web project will need the connection string copied from the class library's app.config.  if the class library is used by some service (like a web service) that lives elsewhere, then the connection string will need to go with that other project.  Having the app.config in the class library can be a bit misleading, but since class libraries are never run on their own (something else always calls them) it's the consuming projects that need the connection string.  Keeping a copy of the app.config with the class provides a source of truth for consumers that need to add the library so the developer will know what to copy to their project's app or web.config.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 19, 2013 5:45 PM
  • User867713863 posted

    thanks for the explanation.  I didn't understand the first sentence but i did understand everything else. So i think i got it! thanks! it was very clear.

    Friday, April 19, 2013 6:00 PM