Tag Archive for Application Settings

C# Modifying a Application Settings Connection String

On the application i have currently been working on, i discovered a small issue. The connection string the application uses is stored as a Application setting. This means that its read-only. Its important to set the connection string as a ConnectionString type within the Application settings so that its secure. But what happens if the connection string changes? This isn’t so bad in a internal application as you can just rollout a new build, but what if each user is likely to have a different connection string.

To change the connection string, help (as usual) came from stack overflow Programmatically change connection string.

That was all fine and dandy but when i went to go and use my application setting by going:

Properties.Settings.Default.ConString;

It returned the old unchanged connection string. the simple way round it was instead of getting my connection string using the code above i had to do this:

System.Configuration.Configuration config2 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
 pconnectionString = config2.ConnectionStrings.ConnectionStrings["ConString"].ToString();

Bingo! that worked!