none
ConfigurationManager Fails From Test Project RRS feed

  • Question

  • I built a console application and it calls ConfigurationManager to retrieve ConnectionStrings.  It works with no problem.  Then I added a test project to the solution (a WinForms project) and when it calls the console class ConfigurationManager.ConnectionStrings on the same line throws the error "Object reference not set".  If I switch the start-up project back to the console application the code runs fine.  Is there anyway to get this to work?  Maybe you can't really call a console class from a WinForms project, but I think it would be nice.


    MCSD .NET developer in Dallas, Texas

    Tuesday, October 23, 2012 8:33 PM

Answers

  • Your app.config is specific to the project it is in and will be copied to the output folder as assemblyname.config. ConfigurationManager only uses the entry point assembly name to locate the .config file. Just copy the app.config to the winforms project, rebuild, and it should just work.
    • Marked as answer by DallasSteve Wednesday, October 24, 2012 1:00 PM
    Wednesday, October 24, 2012 3:15 AM

All replies

  • I would make sure my application had exception handler (Try/Catch) to help determine exactly which line of code is creating the problem.  When the error does occur press the option copy to clipboard and then open Notepad and paste clipboard to get the error trace.

    What normally cause a Console Application to run and a From Application to fial is the owner of the window that is running.  A Consol Appliation has a window open which is uses the read and write user input which has a handle.  In a Form project the form has a handle which is not a window object.  The window handle is the parent of the form.  Any code that requires a window handle will fail on a form project if you use the form handle instead of a window handle.


    jdweng

    Tuesday, October 23, 2012 9:28 PM
  • Your app.config is specific to the project it is in and will be copied to the output folder as assemblyname.config. ConfigurationManager only uses the entry point assembly name to locate the .config file. Just copy the app.config to the winforms project, rebuild, and it should just work.
    • Marked as answer by DallasSteve Wednesday, October 24, 2012 1:00 PM
    Wednesday, October 24, 2012 3:15 AM
  • Hi DallaSteve,

    Please add the config file to your both the projects as Console app as well as Windows Form project. It should work.


    Regards, http://www.shwetalodha.blogspot.in/

    Wednesday, October 24, 2012 7:07 AM
  • Thanks.  Copying app.config to the WinForms project worked.


    MCSD .NET developer in Dallas, Texas

    Wednesday, October 24, 2012 1:00 PM