Using ConfigurationManager breaks WinForms designer...

  • Thread starter Thread starter Charlie@CBFC
  • Start date Start date
C

Charlie@CBFC

Hi:

Program runs and compiles fine, but WinForms designer breaks when using this
line of code...

ConfigurationManager.ConnectionStrings["RepairShop.Properties.Settings.BaseRepairShopConnectionString"].ToString();

It says "Object reference not set to an instance of an object". How then do
I access app.config?

Thanks,
Charlie
 
Charlie,

Can you do this somewhere else other than the constructor of your code?
It's not that the WinForms designer is broken, but rather, you don't have
access to the app config file at that time when the designer is trying to
render your form. An exception gets thrown in the constructor, and the
designer doesn't know what to do.
 
Hi Nicholas:

I'm not using it a constructor. I'm using it in a property like so...

public static string _connectionString = String.Empty;
public static string ConnectionString {
get {
if( _connectionString == String.Empty ) {
_connectionString =
ConfigurationManager.ConnectionStrings["RepairShop.Properties.Settings.BaseRepairShopConnectionString"].ToString();
}
return _connectionString;
}
}

I've tried it in other places without success. I'm at my wits end.


Nicholas Paldino said:
Charlie,

Can you do this somewhere else other than the constructor of your code?
It's not that the WinForms designer is broken, but rather, you don't have
access to the app config file at that time when the designer is trying to
render your form. An exception gets thrown in the constructor, and the
designer doesn't know what to do.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Charlie@CBFC said:
Hi:

Program runs and compiles fine, but WinForms designer breaks when using
this line of code...

ConfigurationManager.ConnectionStrings["RepairShop.Properties.Settings.BaseRepairShopConnectionString"].ToString();

It says "Object reference not set to an instance of an object". How then
do I access app.config?

Thanks,
Charlie
 
Back
Top