views:

327

answers:

1

Hi,

I'm probably not the first one facing this problem, but I couldn't find a proper answer anywhere.

I have a Windows Forms application that uses a strongly-typed DataSet. The designer uses a connection string defined in the application settings. The trouble is that this setting is defined as Application scope (thus read-only), and I need to be able to change it at runtime. In the settings designer, when the type of a setting is "Connection String", it's not possible to change the scope to "User". And the generated dataset doesn't provide a constructor allowing to choose the connection string at runtime, it always uses the one in the settings.

Do you know why MS introduced this restriction? Do you have any workaround?

I'm currently using a workaround that's really ugly: I change the type of the setting to "String", and the scope to "User". That way, I can change it at runtime and it works fine. The trouble is that when I need to modify the dataset in the designer, I have to change it back to "ConnectionString", otherwise the designer doesn't work.

Thanks in advance for your suggestions!

+1  A: 

You can change the value of an ApplicationScope setting at runtime. While the generated and strong-typed property is readonly you can use:

Properties.Settings.Default["App1"] = "bbb";

After that, Properties.Settings.Default.App1 will read "bbb";

This should make it possible to leave the design time setting alone.

You cannot use Settings.Default.Save() for ApplicationScope settings but that is intentional. A normal User does not have the privileges to write in a subfolder of Program Files

Henk Holterman
Thanks for you answer. It's a nice solution, but Properties.Settings.Default.Save() doesn't save this setting... I guess I could save it manually somewhere else and always overwrite it when my app starts.
Thomas Levesque
That's exactly what you should do. A normal user doesn't have the rights to save in \Program Files\
Henk Holterman