C# ConnectionString Update - Read Only?
-
Hi, I fetch my connection string like this:
connectionString = Properties.Settings.Default.KTReunionConnectionString;
Which works perfectly. However I want to give the user the ability to name the database and update the string. Any attempt using the method below gives a compiler message that it's read only. How do I get around this?
Properties.Settings.Default.KTReunionConnectionString = connectionString;
I've tried this, however it doesn't not update in the app.config file:
var mySettings = ConfigurationManager.ConnectionStrings[0];
var cf = typeof(ConfigurationElement).GetField("_bReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
cf.SetValue(mySettings, false);
mySettings.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=" + databaseName + "Integrated Security=True";Is it possible to update the connection string? Thank you,
Glenn
-
Hi, I fetch my connection string like this:
connectionString = Properties.Settings.Default.KTReunionConnectionString;
Which works perfectly. However I want to give the user the ability to name the database and update the string. Any attempt using the method below gives a compiler message that it's read only. How do I get around this?
Properties.Settings.Default.KTReunionConnectionString = connectionString;
I've tried this, however it doesn't not update in the app.config file:
var mySettings = ConfigurationManager.ConnectionStrings[0];
var cf = typeof(ConfigurationElement).GetField("_bReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
cf.SetValue(mySettings, false);
mySettings.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=" + databaseName + "Integrated Security=True";Is it possible to update the connection string? Thank you,
Glenn
This may seem like a great idea, but you have to consider that the app.config file sits in the same folder as your .EXE, under Program Files. The problem with this is under Vista and Win7, users only have read permissions to everything under Program Files. This means that when they run the app, they cannot update the app.config file. Either you have to prompt for and set these things at installation time (which requires an Admin account anyway) or you have to put the settings in a file under the users profile if you're going to allow each user to have their own seperate settings.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi, I fetch my connection string like this:
connectionString = Properties.Settings.Default.KTReunionConnectionString;
Which works perfectly. However I want to give the user the ability to name the database and update the string. Any attempt using the method below gives a compiler message that it's read only. How do I get around this?
Properties.Settings.Default.KTReunionConnectionString = connectionString;
I've tried this, however it doesn't not update in the app.config file:
var mySettings = ConfigurationManager.ConnectionStrings[0];
var cf = typeof(ConfigurationElement).GetField("_bReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
cf.SetValue(mySettings, false);
mySettings.ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=" + databaseName + "Integrated Security=True";Is it possible to update the connection string? Thank you,
Glenn
That would be a reason to not use that config scheme; I always make my own config files.