Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6539915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:54:57+00:00 2026-05-25T10:54:57+00:00

I’m writing a Windows Forms app, VS2010, NET Framework 4.0, coding in VB. I’m

  • 0

I’m writing a Windows Forms app, VS2010, NET Framework 4.0, coding in VB. I’m using the Microsoft Data Connection Configuration dialog in my app to choose a data source. It seems to work fine and creates the ConnectionString correctly. However, the new connection string is not being saved to the app.config file. I did not write the code to save the file and I’m staggering around blindly in this area. Here’s the code I’m using:

  Public Sub SaveConnectionString(ByVal strConnectionName As String, strConnectionString As String)

    Dim Config As Configuration
    Dim Section As ConnectionStringsSection
    Dim Setting As ConnectionStringSettings
    Dim ConnectionFullName As String

    'There is no inbuilt way to change application 
    'setting values in the config file.
    'So that needs to be done manually by calling config section object.

    Try
        'Concatenate the full settings name
        'This differs from Jakob Lithner. Runtime Connection Wizard
        'The ConnectionFullName needs to 
        'refer to the Assembly calling this DLL

        ConnectionFullName = String.Format("{0}.MySettings.{1}", _
            System.Reflection.Assembly.GetCallingAssembly.EntryPoint.DeclaringType.Namespace, strConnectionName)

        'Point out the objects to manipulate
        Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
        Section = CType(Config.GetSection("connectionStrings"),  _
            ConnectionStringsSection)
        Setting = Section.ConnectionStrings(ConnectionFullName)

        MsgBox("To File: " & Config.FilePath.ToString & vbCrLf &
               "Conn. String: " & strConnectionString & vbCrLf &
               "Conn. Name: " & strConnectionName,
               MsgBoxStyle.Information, "Saving Connection String...")

        'Ensure connection setting is defined 
        '(Note: A default value must be set to save the connection setting!)
        If IsNothing(Setting) Then Throw New Exception("There is no connection with this name defined in the config file.")

        'Set value and save it to the config file
        'This differs from Jakob Lithner. Runtime Connection Wizard
        'We only want to save the modified portion of the config file 
        Setting.ConnectionString = strConnectionString
        Config.Save(ConfigurationSaveMode.Full, True)

    Catch ex As Exception

    End Try
End Sub

The MsgBox shows the expected file name and path for the .config file but the file is never updated. I’ve tried this running from the VS2010 IDE, directly running the .exe file created by the compile, creating a Setup and installing the program and running that (program itself runs fine), and installing on a different machine. Always the same – no error messages and the .config file is not updated.

The program is reading the connection string from the appname.exe.config file. If I edit the exe.config file manually I can use different connection strings without problems.

If anyone can offer any guidance, I’ll be VERY appreciative! Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:54:57+00:00Added an answer on May 25, 2026 at 10:54 am

    A fair number of page views to my question, but no answers ventured. Fortunately I found my own answer. Thanks to http://www.thecodemonk.com/2008/02/18/tableadapter-connection-strings for this simple solution.

    Don’t even try to save to the application setting “AppNameConnectionString”. Instead, just save the desired connection string as a user setting, i.e. “ActiveConnectionString” of type string. I replaced the whole block of code I quoted in my original question with this

           My.Settings.ActiveConnectionString = strConn ' previously built conn string
           My.Settings.Save()
    

    What allows this user setting to become the active connection string are the events associated with .Net managing the settings. On the Project settings there is a button “view code” to access the event handlers of the settings. In the SettingsLoaded event, just set the app connection string setting to your user connection string setting:

           Private Sub MySettings_SettingsLoaded(sender As Object, e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded
            Me.Item("MyAppNameConnectionString") = Me.Item("ActiveConnectionString")
        End Sub
    

    That’s all it takes for your user setting to become the working connection string when the app loads.

    Add another event handler in the Settings code and when you save a new connection string from within the app it will be instantly activated!

        Private Sub MySettings_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
            If e.PropertyName = "ActiveConnectionString" Then
                Me.Item("MyAppNameConnectionString") = Me.Item("ActiveConnectionString")
            End If
        End Sub
    

    Thanks again to TheCodeMonk for sharing this stunningly simple, but apparently very elusive solution!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.