5,557,174 members and growing! (16,195 online)
Email Password   helpLost your password?
Languages » VB.NET » HowTo     Intermediate License: The Code Project Open License (CPOL)

How to persist changes to My.Settings.ConnectionString

By mjmeans

This article describes a solution for persisting changes to the ReadOnly My.Settings.ConnectionString with nearly trivial code.
VB 8.0, VB, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 12 Jul 2007
Updated: 1 Aug 2007
Views: 26,854
Bookmarked: 35 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 5.42 Rating: 4.50 out of 5
0 votes, 0.0%
1
1 vote, 7.7%
2
1 vote, 7.7%
3
0 votes, 0.0%
4
11 votes, 84.6%
5

Introduction

Connection strings in the My.Settings class are Application scoped. This causes a problem when deploying an application because the development machine only rarely will have the same connection string as the deployment target machine of your customer. Add to this the fact that strongly typed datasets' table adapters get their connection string from the My.Settings class, and this creates the need for some kind of workaround. The most common workaround I have seen involves setting the TableAdapter.Connection.ConnectionString property with the appropriate connection string before calling Fill(). However, this is non-intuitive for most people, and for very large projects, updating these commands can be problematic.

Here is a better way to persist changes to application scoped connection strings for strongly typed datasets. This will handle any number of connection string overrides you want, with only trivial editing. No need to modify or re-modify the settings.designer.vb file or set TableAdapter.Connection.ConnectionString settings on every table adapter in your project.

Background

How does it work? Well, all the settings, including the application scoped settings, are not kept in memory as read-only. So, the run-time value can be modified, and all future calls to the My.Settings item will get the modified value. What the application scoping does is refuse to persist them. So, this little addition to the MySettings class allows you to:

  1. Update the runtime value of the application scoped setting.
  2. Apply any saved user scoped override setting via the SettingsLoaded event before they are ever referenced by table adapters.
  3. Persist any modified application scoped setting to the user scoped override setting via the SettingsSaving event.

Using the Code

  1. Go to your project Properties, Settings, and make a user scoped string entry for each Application scoped connection string with the same base name plus an added suffix. For example, I use the suffix "UserOverride". If I have two application scoped connection settings called "ConnectionString1" and "ConnectionString2", then I create two user scoped strings (not (connection strings)) called "ConnectionString1UserOverride" and "ConnectionString2UserOverride".
  2. Create a new module called Settings.UserOverride.vb, and insert this code:
  3. Option Strict On
    Option Explicit On
    Namespace My
        Partial Friend NotInheritable Class MySettings
            Inherits Global.System.Configuration.ApplicationSettingsBase
    
            Private Shared userOverrides() As String = { _
                "ConnectionString1", _
                "ConnectionString2" _
            }
    
            Private Shared userOverrideSuffix As String = "UserOverride"
    
            Public Sub SetUserOverride(ByVal [property] As String, _
                                       ByVal value As String)
                Me([property]) = value
            End Sub
    
            Private Sub userOverride_SettingsLoaded(ByVal sender As Object, _
                    ByVal e As System.Configuration.SettingsLoadedEventArgs) _
                Handles Me.SettingsLoaded
                Dim userProperty As String
                For Each appProperty As String In userOverrides
                    userProperty = appProperty & userOverrideSuffix
                    If CType(Me(userProperty), String).Length > 0 Then
                        Me(appProperty) = Me(userProperty)
                    End If
                Next
            End Sub
    
            Private Sub userOverride_SettingsSaving(ByVal sender As Object, _
                    ByVal e As System.ComponentModel.CancelEventArgs) _
                Handles Me.SettingsSaving
                Dim userProperty As String
                For Each appProperty As String In userOverrides
                    userProperty = appProperty & userOverrideSuffix
                    Me(userProperty) = Me(appProperty)
                Next
            End Sub
        End Class
    End Namespace
  4. Edit the userOverrides() array to include the names of the Application scoped connection strings in your application for which you have created user overrides. If you used a different suffix for your overrides, then change the userOverrideSuffix value also.
  5. Now, wherever you need to update your connection string, call SetUserOverride(). For example, whenever you would want to call My.Settings.ConnectionString1 = "My new string", which is not allowed, you instead call My.Settings.SetUserOverride("ConnectionString1", "My new string").
  6. ' This doesn't work because ConnectionStrings are ReadOnly
    'My.Settings.ConnectionString1 = "My new string"
    
    ' This works!
    My.Settings.SetUserOverride("ConnectionString1", "My new string")
  7. Go to your My Project, Application Settings, and make sure that Save My.Settings on Shutdown is enabled.

Points of Interest

Las Vegas! Walt Disnet World Florida! Maybe I'll think of some REALLY interesting ones later. :)

History

  • July 12, 2007: Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mjmeans



Location: United States United States

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 46 (Total in Forum: 46) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralSettings not saving...memberJacquesVorster3:42 6 Aug '08  
GeneralHelpmemberBruno Ricardo Filipe das Neves9:56 8 Jul '08  
GeneralRe: Helpmembermjmeans11:53 8 Jul '08  
QuestionCode variancememberdlcarp11:26 3 Jul '08  
AnswerRe: Code variancemembermjmeans21:40 3 Jul '08  
GeneralRe: Code variancememberdlcarp9:04 4 Jul '08  
GeneralProblemmembercrosurfer3:40 18 Jun '08  
GeneralRe: Problemmembermjmeans10:08 18 Jun '08  
GeneralRe: Problem [modified]membercrosurfer23:31 18 Jun '08  
GeneralRemarkablememberpgraves3:31 9 Apr '08  
GeneralRe: Remarkablemembermjmeans16:37 9 Apr '08  
GeneralPerfect ! Right out of the box.memberDavid Mujica6:40 5 Apr '08  
GeneralThx (and variation)memberMr.PoorEnglish6:06 8 Jan '08  
GeneralRe: Thx (and variation)membermjmeans7:20 8 Jan '08  
Questionwe need some helpmemberdondon987:33 18 Oct '07  
AnswerRe: we need some helpmembermjmeans8:26 18 Oct '07  
GeneralRe: we need some helpmemberdondon9814:28 20 Oct '07  
GeneralRe: we need some helpmembermjmeans23:33 20 Oct '07  
QuestionProblem, It does not work or something missingmemberPhenrir15:41 10 Oct '07  
AnswerRe: Problem, It does not work or something missingmembermjmeans16:27 10 Oct '07  
GeneralRe: Problem, It does not work or something missingmemberPhenrir17:47 10 Oct '07  
GeneralTranslate in C#...memberdevzav2:14 17 Sep '07  
GeneralRe: Translate in C#...membermjmeans8:25 20 Sep '07  
GeneralThanks a lotmemberismailkirkan6:22 12 Sep '07  
GeneralWhy just don't use app.config filememberivan.bolcina22:53 6 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Aug 2007
Editor: Smitha Vijayan
Copyright 2007 by mjmeans
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project