Click here to Skip to main content
15,886,799 members
Articles / Web Development / ASP.NET
Tip/Trick

How to get Connection String from App.Config in C#

Rate me:
Please Sign up or sign in to vote.
4.79/5 (49 votes)
15 Oct 2013CPOL 733.9K   32   20
This code helps you to grab ConnectionString from App.config.

Introduction

We always use Connection String in forms that we design but when you design a big software you should set the connection string for once and use it in many forms. In this method you use the name of the Connection String instead of the connection string text. 

Every time you want to change the connection string just change the main connection string in the App.Config file.

By this method you don't need to change all of the forms in your project, just change the Connection String in the App.Config.

Using the Code

First of all you should set the connection string in the App.Config file. For example I set the connection string for my database as you see here:

XML
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="CharityManagement" 
    connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
  </connectionStrings>
</configuration> 

After that you use the connection string in your forms using this code:

In your forms you set references that you want to use:

C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

Then you can get the Connection String from the App.Config by using the ConnectionStrings property.

C#
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;

You can use this method in both Windows Forms and ASP.NET projects.

License

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


Written By
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
I like programming because when you design a software you create a thing that improve your life.

Comments and Discussions

 
QuestionNice, quick and clean Pin
James McCullough2-Apr-16 16:21
professionalJames McCullough2-Apr-16 16:21 
Suggestionbetter option Pin
Arkadeep De15-Oct-14 20:26
professionalArkadeep De15-Oct-14 20:26 
QuestionCONNECTIONSTRING PROBLEM Pin
Rani Chehadeh2-Oct-14 7:25
Rani Chehadeh2-Oct-14 7:25 
WHEN I AM TRYING to connect to the connectionstring in the app.config
it out a line under configurationmanager and said it does not exist although i have put the right reference !
please i need help
AnswerRe: CONNECTIONSTRING PROBLEM Pin
tara_sh50025-Aug-15 15:02
tara_sh50025-Aug-15 15:02 
GeneralMy vote of 5 Pin
srilekhamenon15-Mar-14 0:17
professionalsrilekhamenon15-Mar-14 0:17 
SuggestionUse repository pattern instead Pin
Uldis Rāts17-Oct-13 0:04
professionalUldis Rāts17-Oct-13 0:04 
GeneralMy vote of 5 Pin
JasonMacD15-Oct-13 11:03
JasonMacD15-Oct-13 11:03 
GeneralRe: My vote of 5 Pin
Sacha Barber15-Oct-13 21:54
Sacha Barber15-Oct-13 21:54 
GeneralRe: My vote of 5 Pin
JasonMacD16-Oct-13 2:32
JasonMacD16-Oct-13 2:32 
GeneralMy vote of 5 Pin
ho.kurd27-Aug-13 12:52
ho.kurd27-Aug-13 12:52 
GeneralMy vote of 1 Pin
csd21-May-13 22:13
csd21-May-13 22:13 
GeneralRe: My vote of 1 Pin
JasonMacD15-Oct-13 11:02
JasonMacD15-Oct-13 11:02 
GeneralRe: My vote of 1 Pin
tlobig14-Apr-15 21:11
tlobig14-Apr-15 21:11 
GeneralMy vote of 5 Pin
Member 850885518-Feb-13 1:19
Member 850885518-Feb-13 1:19 
NewsThe problem of the location of the app.config file. Pin
AmitMukherjee13-Feb-13 23:46
AmitMukherjee13-Feb-13 23:46 
SuggestionAdd Reference Pin
leonelbeira6-Jan-13 4:45
leonelbeira6-Jan-13 4:45 
QuestionIf you face Problem Pin
Karunesh Kumar Verma19-Sep-12 1:22
Karunesh Kumar Verma19-Sep-12 1:22 
AnswerRe: If you face Problem Pin
Venkat Raghvan10-Oct-13 21:33
Venkat Raghvan10-Oct-13 21:33 
AnswerRe: If you face Problem Pin
Alireza C15-Oct-13 10:35
Alireza C15-Oct-13 10:35 
BugRe: If you face Problem Pin
Member 85834414-Oct-17 20:49
Member 85834414-Oct-17 20:49 

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.