Question : how do I access and manipulate the connectionString in app config?

I want to access the connectionString in appconfig so that I won't use the hardcoded string in my windows forms. Also, how do I manipulate the connection string? I plan to make the user of the application create a new db (Ms Access 2003) that's why the connectionString will change when that happens, right? How do I do these? Thanks in advance. Btw I'm using C# .NET Windows Forms and MS Access 2003.

Answer : how do I access and manipulate the connectionString in app config?

using ConfigurationManager won't do any good cause u get ConfigurationErrorsException if u try to change setting saying: "[System.Configuration.ConfigurationErrorsException] = {"The configuration is read only."}"

u would have to handle the conection string yourself.
u can still use the app.config to store/modify connection string properties, however, u will have to create a custom configuration manager which will use other methods to access the app.config and update the connection string.
since app.config is xml based file u can parse it as one, using XElement,XmlDocument etc.
or better yet u can load the app.config explicitly using ConfigurationManager.OpenMappedExeConfiguration() and then add/modify connection string properties:


                Configuration exConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                {
                    ExeConfigFilename = @"c:\temp\app.config"
                }, ConfigurationUserLevel.None);
                exConfiguration.ConnectionStrings.ConnectionStrings[0] = new ConnectionStringSettings("","NECTION_STRING>","VIDER>");




Random Solutions  
 
programming4us programming4us