Forms designer error when trying to display

I have a Windows Forms project in a VS 2005 solution along with a Class Library project. The class library contains a class with the following static method:

public static void LogError(string strSource, string ErrMessage)
{
//write the log message to the log
if ( !EventLog.SourceExists(ConfigurationManager.AppSettings["ApplicationName"]) )
{
EventLog.CreateEventSource(ConfigurationManager.AppSettings["ApplicationName"], CommonConstants.ErrorLogName);
}

...some more functionality
}

Now, in my forms project, which references the class library, I have an inherited form that throws the following error message when I have the lines from above that call ConfigurationManager not commented out. And yes, the "ApplicationName" element does exist in the configuration file.

One or more errors encountered while loading the designer. The errors are listed below. Some errors can be fixed by rebuilding your project, while others may require code changes.

Must specify value for source.

Hide

at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)
at System.Diagnostics.EventLog.CreateEventSource(String source, String logName)
at CommonFramework.Utils.ErrorHandling.LogError(String strSource, String ErrMessage) in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\CommonFramework\CommonFramework\Utils\ErrorHandling.cs:line 26
at CommonFramework.Utils.ErrorHandling.LogError(String strSource, String ErrMessage, Object[,] alParams) in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\CommonFramework\CommonFramework\Utils\ErrorHandling.cs:line 56
at CommonFramework.Utils.DataAccess.LoadTable(String strStoredProc, Object[,] alParameters, String[] strTableName) in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\CommonFramework\CommonFramework\Utils\DataAccess.cs:line 64
at ConfigurationUtility.DrawListing.LoadReferenceData() in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\ConfigurationUtility\Forms\DrawListing.cs:line 352
at ConfigurationUtility.DrawListing..ctor() in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\ConfigurationUtility\Forms\DrawListing.cs:line 53
at ConfigurationUtility.Global..cctor() in C:\development\gaming\Games\Carded Jackpots\DotNet 2005\ConfigurationUtility\Common\Global.cs:line 14
So, is there any way to resolve this If I comment out the offending line in the class library project, or I hardcode the value, then it's fine. And when I run the project the screen appears with no problems. I just can't view it in the designer.


Answer this question

Forms designer error when trying to display

  • Adrigo Gallus

    You should be aware of the fact that the windows forms designer will actually create an instance of your control when you put it on the design surface, so any code that is called from your constructor will run in the context of Visual Studio. What this means is that you are in fact trying to get the ApplicationName app setting from devenv.exe.config, which is probably not what you want to do.

    Now, the good thing about this is that you found what is (in my opinion) an issue in your code - you shouldn't assume that your application is configured correctly since an evil administrator may have changed your app.config file post deployment . Having a slightly more friendly error message may be preferred to the "Must specify value for source"

    Check out http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/default.aspx for more information.

    Best regards,
    Johan Stenberg



  • jw700

    I've just found a workaround of sorts. If I wrap the function in a null check like this:

    if ( ConfigurationManager.AppSettings["ApplicationName"] != null )
    {
    f ( !EventLog.SourceExists(ConfigurationManager.AppSettings["ApplicationName"]) )
    {
    EventLog.CreateEventSource(ConfigurationManager.AppSettings["ApplicationName"], CommonConstants.ErrorLogName);
    }

    ...some more functionality

    }

    Then it works. Not a good enough solution though in my opinion. You shouldn't have to do that to be able to use the designer.

  • Forms designer error when trying to display