Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Managing Application Scoped Connection Strings

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
25 Dec 2011CPOL2 min read 19.8K   1   2
Managing Application Scoped Connection Strings
If you haven't used DataSets and TableAdapters in your WinForms application, you probably will at some point. As with everything, there are positives and negatives. One big negative I experienced (and maybe you will too), is managing those darn connection strings. So let's get started.

After hard work, long hours spent finishing your software product, it's time to get that application to its end destination. By now, you would've noticed that the connection strings used for your DataSets cannot be changed during runtime (according to Microsoft (and several other articles and blogs)). Ultimately, any and all connection strings are "Application Scoped" which just simply means that whatever value is contained within that specific setting, is used for the entire application (or "globally" if you prefer). Other settings (usually those set to "User Scoped") are managed on a per-user basis. In other words, User A will have his/her setting values, and User B will have his/her setting values. With Application Scoped settings, User A and User B will share the same setting and its value.

Many articles out there claim that these values ("Application Scoped") cannot be changed during runtime. Some give you pages and pages of code to get the settings to change. Some even provide a workaround by overriding the actual setting with a temporary setting. Well, I'm here to give you the ultimate solution. Before I show you how, you need to understand why people claim that "Application Scoped"-settings cannot be changed: When your application is launched, these "Application Scoped" settings are loaded and used for the duration of the application session. Once the application exits, the "lock" or "read-only" status is removed from these settings.

Unlike normal settings that can be accessed via My.Settings (VB) or Properties.Settings.Default (C#), the application scoped settings requires the use of the ConfigurationManager.

IMPORTANT: In order to use the Configuration Manager, you need to import the namespace in the code file AND add the reference to your project (VB: Project > Settings > References / C#: Right-click References).

Note: For this article, I will be providing the source code in C#. It is easily adaptable to VB.NET or you can simply convert the code using an online converter.

After you have added the reference to System.Configuration, import the namespace into your code file:

C#
using System.Configuration;


Now, you need to open the settings, make the changes, save it, then refresh the settings so that it reflects the new value:

C#
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connStringName"].ConnectionString = "New conn string";
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");


  • ["connStringName"] refers to the name of the setting (you can find this by opening your Settings);
  • "New conn string" - You should put the new connection string you would like to use here.
  • ("connectionStrings") should not be changed under any circumstances (if you change it, the application will fail to register the connection string).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
South Africa South Africa
I was a software and web application developer for several local companies and started my own company on the side providing similar services. I used to develop using Microsoft-technologies such as Microsoft C#, Microsoft .NET, Microsoft ASP.NET and MVC, and Microsoft SQL Server.

In 2015 I have started pursuing a career in aviation and am currently studying for my Student Pilot License after which I will obtain my PPL and CPL.

Comments and Discussions

 
GeneralI am re-posting, since my first was done as an 'alternative ... Pin
SteveMets6-Dec-11 8:51
professionalSteveMets6-Dec-11 8:51 
GeneralRe: Hi, What is the exception you get? And where does this happ... Pin
Juan Davel6-Dec-11 18:50
professionalJuan Davel6-Dec-11 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.