Click here to Skip to main content
13,000,324 members (72,812 online)


QuestionC# use connection string in app.config Pin
rachel_m13-Nov-12 11:46
memberrachel_m13-Nov-12 11:46 
Can you tell me how to use the connection string value from the app.config file and not the hard codee values?

In a C# 2008 desktop/console application, I created the connections to the database using linq to sql. Everything worked fine until I moved the
code to point to a different database. Somehow the original database connections are being saved and the values from the app.config file
are not being used.

I tried to do what the following link:,
said to do however, I do not know what the following statement means to do from this link:
Now Right Click and open the Properties on your DAL or project containing your LINQ to SQL classes and remove the connection string “Application Setting” reference on the Settings tab.

I also tried to add the following to the *.designer.cs file:
base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.devtestConnectionString"].ConnectionString, mappingSource) and attached
a system.configuration file as a reference.

1. The following code is from the app.config file:
    <add name="esample.Properties.Settings.devtestConnectionString"
      connectionString="Data Source=dev;Initial Catalog=devtest;Integrated Security=True"
      providerName="System.Data.SqlClient" />    
2. The following is from the settings.settings file
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="" CurrentProfile="(Default)" GeneratedClassNamespace="esample.Properties" GeneratedClassName="Settings">
  <Profiles />
    <Setting Name="devtestConnectionString" Type="(Connection string)" Scope="Application">
      <DesignTimeValue Profile="(Default)"><?xml version="1.0" encoding="utf-16"?>
<SerializableConnectionString xmlns:xsi="" xmlns:xsd="">
  <ConnectionString>Data Source=dev;Initial Catalog=devtest;Integrated Security=True</ConnectionString>
      <Value Profile="(Default)">Data Source=dev;Initial Catalog=devtest;Integrated Security=True</Value>
The following is from the settings.designer.cs file
namespace esample.Properties {
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
        public static Settings Default {
            get {
                return defaultInstance;
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=dev;Initial Catalog=devtest;Integrated Security=True")]
        public string devtestConnectionString {
            get {
                return ((string)(this["devtestConnectionString"]));
The following is from the *.designer.cs
namespace esample
	using System.Data.Linq;
	using System.Data.Linq.Mapping;
	using System.Data;
	using System.Collections.Generic;
	using System.Reflection;
	using System.Linq;
	using System.Linq.Expressions;
	using System.ComponentModel;
	using System;
        using System.Configuration;
	public partial class esampleDataContext : System.Data.Linq.DataContext
		private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
		public esampleDataContext() :
        base(global::esample.Properties.Settings.Default.devtestConnectionString, mappingSource)
        //base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.devtestConnectionString"].ConnectionString,  mappingSource)
		public esampleDataContext(string connection) : 
				base(connection, mappingSource)

Can you show me code and/or tell me how to solve this problem so I can obtain the value from the app.config file?
AnswerRe: C# use connection string in app.config Pin
Clifford Nelson13-Nov-12 11:55
memberClifford Nelson13-Nov-12 11:55 
GeneralRe: C# use connection string in app.config Pin
rachel_m13-Nov-12 17:45
memberrachel_m13-Nov-12 17:45 
GeneralRe: C# use connection string in app.config Pin
Pete O'Hanlon13-Nov-12 19:39
protectorPete O'Hanlon13-Nov-12 19:39 
AnswerRe: C# use connection string in app.config Pin
Clifford Nelson14-Nov-12 5:08
memberClifford Nelson14-Nov-12 5:08 
AnswerRe: C# use connection string in app.config Pin
n.podbielski13-Nov-12 20:24
membern.podbielski13-Nov-12 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 25 Jun 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid