Using Dynamic Properties in Windows Forms


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.

<?xml version="1.0" encoding="Windows-1252"?>
<
configuration>
<appSettings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> -->
<add key="myConnectionString" value="data source=myServer;initial
catalog=Northwind;persist security info=False;user id=sa;workstation
id=HOMEDOT;packet size=4096"
/>
</appSettings>
</
configuration>

Xml listing: app.config file

Visual Studio.Net also adds code to initialize the ConnectionString property of the SqlConnection object to the value from the config file corresponding to the key defined in the dynamic properties:

this.sqlConnection1.ConnectionString = ((string)(configurationAppSettings.GetValue"myConnectionString", typeof(string))));

Now that the ConnectionString is setup as a dynamic property, you can modify the ConnectionString value in the app.config file at outside the project. This is very useful in administration, deployment and maintenance. When you compile the Visual Studio.Net project, Visual Studio creates a file with the name <appname>.exe.config in the \bin or the \obj\debug\ folder of the project folder, where <appname> is replaced by the name of the windows application. You can modify the values in this config file to change the value of the dynamic properties without needing to recompile the application. You can test this by changing the value of the connection string in the appname.exe.config file and running the executable compiled for the project.

NOTE: This article is purely for demonstration. This article should not be construed as a best practices white paper. This article is entirely original, unless specified. Any resemblance to other material is an un-intentional coincidence and should not be misconstrued as malicious, slanderous, or any anything else hereof.

Conclusion

In this example we learnt how to dynamically configure properties in a Windows application using Visual Studio.Net and the Application Configuration file.


Similar Articles