views:

78

answers:

2

Please help, for the sake of my non-pulled out hair...

The following code line:

this._connectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;

is causing me untold amounts of grief.

It is in a user control, currently in the control's Loaded event but I've also tried the constructor and just plain initializing the field to the value when it's declared. Whenever I do so, the WPF designer pitches a fit on any screen that uses said user control.

The code itself compiles fine, and runs with no issues. But it's turning into a real hampering in development not being able to use the designer at all. Does anyone have any clue what could cause this and a hint as to a good practice to avoid it in the future? I suspect it has something to do with trying to access the ConfigurationManager but I can't figure out where to put the line to make it stop.

Thanks.

PS: Visual Studio 2010 Premium

+1  A: 

The designer probably isn't looking at your app's configuration but rather at the configuration of its current host app (VS), and so ConfigurationManager.ConnectionStrings["SqlConnectionString"] returns null at design-time.

500 - Internal Server Error
+1  A: 

When you're working in design time, you should avoid loading this. Fill in the value with some other, appropriate default, instead:

if (DesignerProperties.GetIsInDesignMode(this))
    this._connectionString = "Default";
else
{
    this._connectionString = ConfigurationManager
                              .ConnectionStrings["SqlConnectionString"]
                              .ConnectionString;
}
Reed Copsey
Not working. It still keeps going and tries to parse "Default" and then says unable to parse or some nonsense. So I put in a valid but broken connection string and it actually attempted to connect with it and failed.
zk812
@zk812: You need to make your connection not connect at design time, as well.
Reed Copsey
How has this never been a problem before VS2010? All my VS2008 WPF apps follow this same pattern with no issue...
zk812
@zk812: This isn't typically a problem for most controls, as they rarely "do" something at design time. You're trying to connect to a DB at load time (which is not a very good WPF thing to do - MVVM would help a lot here), which causes VS to try to connect at design time.
Reed Copsey