Imports System.Data.SqlClient Imports System.Text Imports System.Configuration Imports System.Xml Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim strConnectionStringName As String = "con" Dim Con As StringBuilder = New StringBuilder("Data Source=") Con.Append(txtServer.Text) Con.Append(";Initial Catalog=") Con.Append(txtDatabase.Text) Con.Append(";Integrated Security=SSPI;") Con.Append("User ID=username; Password=xyz;") Dim ConStringSettings As New ConnectionStringSettings(strConnectionStringName, Con.ToString) Dim Config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) AddAndSaveOneConnectionStringSettings(Config, ConStringSettings) Catch ex As Exception MessageBox.Show(ConfigurationManager.ConnectionStrings("con").ToString() & _ "This is invalid connection", "Incorrect server/Database.") End Try End Sub Public Sub AddAndSaveOneConnectionStringSettings(ByVal Config As Configuration, _ ByVal ConStringSettings As ConnectionStringSettings) Dim ConStringsSection As ConnectionStringsSection = Config.ConnectionStrings ConStringsSection.ConnectionStrings.Add(ConStringSettings) Config.Save(ConfigurationSaveMode.Minimal) ConfigurationManager.RefreshSection("connectionStrings") End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)