Monday, August 23, 2010

Using web.config To Store Connection String

So, maybe this was written about about 100K times, however, I thought it would be good to add it again as I just helped someone add this functionality

You can add any connection string, this example will show for Microsoft Access - yes people still use this database.


  1. Open the web.config file.
  2. If you do not see a section called , then add one. The section should be inside the section.
    You may see , if this is the case erase this line and start typing:
    . If you have code sense turned on, very quickly you should be able to hit the key and you will end up with

    If so, put the cursor between the ">" and the "<" symbols and hit enter. You will end up with

    start typing here
  3. Okay, in the "start typing here, area, insert:

    I like to add this line to ensure my connection is not duplicated as I add it
  4. Now, just below this line, enter this line:
  5. Note the "conMyConnection" from the remove name and the add name should match
  6. Now, in your VB code, you can define a string such as:
    Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("conMyConnection").ConnectionString.ToString)
  7. Note again the "conMyConnection" matches all the way through
  8. So, if you need to change the location of your database, just change the web.config file and all calls to the database will find the new path.
Happy Databasing

Daniel