Modify app.config at runtime

I am writing a rather complex application. The Application connects to a SQL Server Database to retrieve and store information.

I usually work on the code either on my laptop or my desktop computer at work. I have a copy of SQL Server on each of the machines with a copy of the database backup that I keep up to date and update on both machines when I make any changes.

SQL Server on Laptop : MOBILESERVER\SQLDB Catalog=FileStoreDB
SQL Server on Desktop: ITSERVER\SQLDB Catalog=FileStoreDB

Now, I would like a way to modify the connection string in the app.config at run time. So that I can simply pick from a drop down list whether I am at work or at home and the application would use the appropriate connection string.

I know how to write the IF statement to modify the string. I want to know how to modify the app.config to accept the change and apply it. Or if there's another way to do that?

any help would be appreciated.

Which Declarations and Which properties or methods do I need to use?


Private Sub cboLocation_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboLocation.SelectedIndexChanged
 
        If cboLocation.Text = "Home" Then
            'Code Should Go here
        ElseIf cboLocation.Text = "Work" Then
        
        End If
 
    End Sub

Open in new window

LVL 5
Malik1947Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
I tied to do this in VB6 until I realised that there is no such thing in that version, and that I need .NET to test it.

However this site seems to think it is possible and shows how.

http://geekswithblogs.net/akraus1/articles/64871.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
"I tied to do this" should read "I tried to do this"
0
dqmqCommented:
You don't need to modify app.config at runtime.   Instead, add both connection strings permanently to app.config.  Find the place in your code where the connection string is retrieved from app.config.  Then retrieve one or the other depending on what's selected in the dropdown.
0
rsomasundarCommented:
You can switch the connection string by this way..

I assumed the aspx file has dropdownlist which provides option to choose the environement and one button to establish the connection..

You can follow your rest of codes..

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            ddlEnv.Items.Add("HOME")
            ddlEnv.Items.Add("OFFICE")
        End If
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Dim strConnection As String
        Dim sqlCon As New SqlConnection
        sqlCon.ConnectionString = GetConnectionString(ddlEnv.SelectedValue)
        sqlCon.Open()
    End Sub


    Private Function GetConnectionString(ByVal strEnvironment) As String
        Dim strConn As String
        If strEnvironment = "HOME" Then
            strConn = "Server=MOBILESERVER\SQLDB;Initial Catalog=FILESTOREDB;" _
               & "Integrated Security=SSPI;"
        ElseIf strEnvironment = "OFFICE" Then
            strConn = "Server=ITSERVER\SQLDB;Initial Catalog=FILESTOREDB;" _
               & "Integrated Security=SSPI;"
        End If
        Return strConn
    End Function
0
Éric MoreauSenior .Net ConsultantCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.