Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Generate Configuration file through System.Configuration in C# and VB using .NET 2.0

Author: Zunnair
Download Source Code : 733_APP_Config_working.zip

In this simple article you will learn that how to Generate Configuration file through System.Configuration NameSpace

Simply just create New Project of windows application in C#.

 

Drag 2 buttons, four labels and four textbox on the form.

 

Now write following code on form Load

 

C#

 

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp App Config Generator";

            

            txtserver.Text = "DB Server Name";

            txtDb.Text = "DB Name";

            txtUser.Text = "DB User";

            txtPassword.Text = "DB Password";

            textBox1.Text = "APP.Config";

 

        }

 

VB

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DevAsp App Config Generator"

 

        txtserver.Text = "DB Server Name"

        txtDb.Text = "DB Name"

        txtUser.Text = "DB User"

        txtPassword.Text = "DB Password"

        textBox1.Text = "APP.Config"

 

    End Sub

 

Now write EditConnectionString function

C#

public bool EditConnectionString(string filename, string servername, string username, string password, string DB)

        {

            try

            {

                System.Configuration.ExeConfigurationFileMap con = new System.Configuration.ExeConfigurationFileMap();

                con.ExeConfigFilename = filename;

 

                System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(con, ConfigurationUserLevel.None);

                config.ConnectionStrings.ConnectionStrings.Clear();

               

                ConnectionStringSettings connStrSettings = new ConnectionStringSettings("CON_STR1", "Initial Catalog=" + DB + ";Data Source=" + servername + "; User ID=" + username + "; Password=" + password + ";");

                config.ConnectionStrings.ConnectionStrings.Add(connStrSettings);

                config.Save();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            return true;

        }

VB

Public Function EditConnectionString(ByVal filename As String, ByVal servername As String, ByVal username As String, ByVal password As String, ByVal DB As String) As Boolean

        Try

            Dim con As New System.Configuration.ExeConfigurationFileMap()

            con.ExeConfigFilename = filename

 

            Dim config As System.Configuration.Configuration = ConfigurationManager.OpenMappedExeConfiguration(con, ConfigurationUserLevel.None)

            config.ConnectionStrings.ConnectionStrings.Clear()

 

            Dim connStrSettings As New ConnectionStringSettings("CON_STR1", "Initial Catalog=" + DB + ";Data Source=" + servername + "; User ID=" + username + "; Password=" + password + ";")

            config.ConnectionStrings.ConnectionStrings.Add(connStrSettings)

            config.Save()

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

        Return True

    End Function

Now write code on Button click event

 

C#

 

private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                EditConnectionString(textBox1.Text.Trim(), txtserver.Text.Trim(), txtUser.Text.Trim(), txtPassword.Text.Trim(), txtDb.Text.Trim());

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

 

VB

 

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Try

            EditConnectionString(textBox1.Text.Trim(), txtserver.Text.Trim(), txtUser.Text.Trim(), txtPassword.Text.Trim(), txtDb.Text.Trim())

        Catch ex As Exception

            MessageBox.Show(ex.Message)

        End Try

    End Sub

    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        Application.[Exit]()

    End Sub

 

 

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Textbox Finalization with Enter Key in C# and VB using .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net