Add new connection string to your web application:

Dec 28 2009 3:22 AM


this topic about adding new connection string to the application at run time.

the user will fill the (connection name, machine name or IP, database name, login ID, login password)


Add this code to the button  event handler:

string connectionStringName = txtNewConn.Text;
                string connectionString = "Password=" + txtPassword.Text + ";Persist Security Info=True;User ID=" + txtUserLogin.Text + ";Initial Catalog=" + txtDBName.Text + ";Data Source=" + txtIP.Text;

                ConnectionStringSettings Settings = new ConnectionStringSettings(connectionStringName, connectionString);
                Settings.ProviderName = "System.Data.SqlClient";

                var cfg = WebConfigurationManager.OpenWebConfiguration(@"/");
                cfg.ConnectionStrings.ConnectionStrings.Add(Settings);

                cfg.Save();

Answers (2)