Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to Change web.config ConnectionString dynamically

$
0
0

Hi, 

I want to be able to change my web site web.config dynamically so that any time i made changes to the application i don't need to download the web config and re-upload, i have tried the following

// ===========================================
        // The content of pathname is shown below
        //  string pathname = "~/web.config";
        //============================================

        System.Configuration.Configuration Config1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(pathname);
        ConnectionStringsSection conSetting = (ConnectionStringsSection)Config1.GetSection("connectionString");
        ConnectionStringSettings StringSettings = new ConnectionStringSettings("NIQSDB", "Data Source=" + ServerName + ";Database=" + DBaseName + ";User ID=" + UserID + ";Password=" + PSSWD + ";");
        conSetting.ConnectionStrings.Remove(StringSettings);
        conSetting.ConnectionStrings.Add(StringSettings);
        Config1.Save(ConfigurationSaveMode.Modified);  

and i get the following error :

Object reference not set to an instance of an object.

 at 

conSetting.ConnectionStrings.Remove(StringSettings);
please what am i doing wrongly.
thank you

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>