Customizing the connection string for TableAdapters

Visual Studio 2005 now leverages the strongly typed settings file which wraps app.config.  With connection strings stored in app.config/Settings, developers can centralize their connection strings across their application.
Some advantages over VS2003 are:

  • Strongly typed settings file. 
    Rather then having to write AppSettinsReader.GetValue(“NameOfConfigItem”) as Object you can simply write:
    conn.ConnectionString = My.Settings.NorthwindConnectionString 
    The new settings file is compiled by name so you get intellisense as well as compile time verification if you ever change the name.  You also get strong types so you know if the setting returns a string, bool, int, or bitmap.
  • Default Values
    Often developers want to ship their app with some default value.  In 2003 developers had to ship their app.config file just to get the default.  If the end user ever mucked with this value there wasn’t any way to get back to the default.  In 2005, VS now generates a DefaultValue directly in the code using the System.Configuration.DefaultSettingValueAttribute
    With this attribute, developers no longer have to ship anything in the app.config file.  When Settings loads, if it finds a value in app.config, it will use it.  If no value is found, it will use the DefaultSettingsValue.  Not only does this help runtime scenarios, but VS can now host components in the component tray that utilize app.config.  In VS2003, this would cause the “white screen of darn”

Customizing the connection string at runtime
Developers often want to customize the connection string.  In most cases the password simply needs to be provided.  The strongly typed Settings file also has a few events that can be leveraged. 

  1. To add an event handler:
  2. Double click My Project in Solution Explorer
  3. Click the Settings tab on the left side
    In C#, double click the Properties folder in Solution Explorer
  4. Click the View Code toolbar in the top of the designer
  5. Select MySettings Events from the top left drop down and then select SettingsLoaded in the top right.

Here’s a snippet I user for SQL Mobile to replace .\ with the data directory when running under click once.  This same code can be modified to replace the password of a connection string.

Private Sub Settings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded


    Dim dataDirectory As String


    ‘ When running under debug mode, use the data file from the same directory as the executable

    ‘ otherwise, use the Data Directory which will be set for ClickOnce or MSI based installs

    ‘ This assumes that MSI based apps placed the data file in the standard non roaming data directory:

    ‘ C:\Documents and Settings\<UserName>\Application Data\<Company>\<Application Name>\<Version>

    If AppDomain.CurrentDomain.DomainManager IsNot Nothing AndAlso _

        AppDomain.CurrentDomain.DomainManager.ToString().Contains(“VSHost”) Then


        dataDirectory = Windows.Forms.Application.StartupPath


        dataDirectory = Windows.Forms.Application.UserAppDataPath

    End If


    Me.Item(“NorthwindConnectionString”) = My.Settings.NorthwindConnectionString.Replace(“.\”, dataDirectory & “\”)


End Sub

Steve Lasker


Comments (15)

  1. Bill Vaughn says:

    If we want to encrypt/decrypt the connection string, are there any suggested "best practice" techniques to use?

  2. MDeevi says:

    The DataAdaptor in Beta 1 had a generate WebMethod property. In Beta 2 this property isn’t there. I want to know how to web-enable the dataset and update data across the web using the DataSet designer rather than hand coding it, the way I could use the generateWebMethod property.

    Please let us know whether this feature will be available in VS2005 RTM product.

  3. Overview

    In Visual Studio 2005 we added a number of features to help developers build and deploy applications…

  4. Overview

    In Visual Studio 2005 we added a number of features to help developers build and deploy applications…

  5. In instruction "To add an event handler" step 5 is applicable for VB only.

    AFAIK Visual Studio doesn’t help to create c#  event handler and you should do it manually.

  6. Actually "To create an event handler in C#"


    Create the event delegate in the Constructor of the class by typing the qualified event name followed by a space, and then typing += with no space afterwards. For example:

    this.<object name>.<event name> +=

    At the end of the line of code, press the TAB key twice.

    Visual Studio automatically completes the line of code, creates the event handler, and moves the insertion point to the newly created event handler.

  7. My application needs to be installed from a network location and each user needs to access data from a single network database file (Access by OLEDB). This location is not (and is never likely to be) the same as the location used for development!

    I cannot believe that any other application is going to need a hard-coded path to its data. Yet, if one is creating a Click-Once installation, that is exactly what happens. If one installs the installation files on a new network, then one cannot edit the application.exe.config file in the deployment as the hash value is then different and the installation fails.

    I have just spent an entire day attempting to set the |DataDirectory| location at run-time. Eventually, I made a work-around in which I edited the application.exe.config file directly from code when my startup form was loading, saved the changes, then restarted the application.

    Why on earth was the fact that the |DataDirectory| variable is merely a property of the AppDomain not included in the Visual Studio documentation? Furthermore, why has the on-line documentation not been updated to include this crucial piece of information?

  8. Ashkan says:

    I’m a solo programer. I’ve create a new C# project in VS 2005. I’ve intall SQL Server 2005 Ent. eddition on my localhost and I’m using it as my database server.
    At VS in “server Explorer” window I create a new data connection to my sql server. Test and OK!
    At next step I create a new sqlconnection object to use it in my application. I don’t want to use dataset, I need just to add data to my table manually.
    Every thing is good.
    but now I’m trying to deploy my very simple application to destination computer on a network. I JUST NEED TO CHANGE MY COMPUTER NAME TO SQL SERVER NAME ON THAT NETWORK… WHAT SHOUL I DO?
    I tried to change .Config file, but it seems these files are using when I’m using “Data Source” in VS2005.
    Thank you.

  9. si es que estamos usando SQLExpress, y estamos creando una aplicación .NET para el acceso a datos, hay…

  10. si es que estamos usando SQLExpress, y estamos creando una aplicación .NET para el acceso a datos, hay

  11. Modificare le Settings con scope Application

  12. 老许 says: