In this article we will see how to dynamically configure properties in Windows Applications using Visual Studio.Net. For this sample, we will dynamically configure our SQLConnection objects. The connection string will be stored in the application's config file. Visual Studio provides inherent support for dynamic properties.
Uses
Before we go into the details of using dynamic properties, let's take a look at the areas in which dynamic properties can be applied.
The biggest advantage of dynamic properties is that the value of the property can be modified outside the project by administrators. This is very useful in deployment and maintenance scenarios.
Dynamic properties can be used anywhere you need configurability. You can make use of dynamic properties in configuring database connections, event logs, performance counters, log files etc.Note the difference between configuration files and resource files. Config files are used for updating properties whereas resource files are used in translation activities.
Details
In Visual Studio.Net, create a Visual C# Windows Forms Application. Drag and drop two connection objects from the Data tab of the ToolBox to the Windows Form Form1.
Figure: SQLConnection Objects
Click on the SQLConnection1 object. In the Properties window, open the Dynamic Properties node. ConnectionString is available as a dynamic property by default.
Figure: Dynamic Properties node for the SqlConnection object.
Click on the elipses next to the ConnectionString Dynamic Property.
We will map the dynamic property to a key named "myConnectionString". Make sure that the Map property to a key in the configuration file is checked.
Clicking OK generates the application config file , app.config, (if it does not exist) and adds the xml for the key. We can add the value for the string in the value attribute of the connection string.
<?