Can Of Code

C# Modifying a Application Settings Connection String

On the application I have currently been working on I discovered a small issue. The connection string was stored as a Application setting which meant it was 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 the application setting by going:

Properties.Settings.Default.ConString;

It returned the old unchanged connection string. the simple way round it was instead of getting the 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!

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *