Get connection string from App.config

var connection = ConnectionFactory.GetConnection(
    ConfigurationManager.ConnectionStrings["Test"]
    .ConnectionString, DataBaseProvider);

And this is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="Test" connectionString="Data Source=.;Initial Catalog=OmidPayamak;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

But when my project runs this is my error:

Object reference not set to an instance of an object.

Can't you just do the following:

var connection = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["Test"].ConnectionString;

Your assembly also needs a reference to System.Configuration.dll

How to get Connection String from App.Config in C#, Then you can get the Connection String from the App.Config by using the ConnectionStrings property. Hide Copy Code. var connectionString=  it's solved! the app.config was in the same project as the class.cs, but for testing purposes I had created a new one with a windows form containing a datagrid with a simple select * from table from the desired server. I had to put the app.config inside that one project.

Since this is very common question I have prepared some screen shots from Visual Studio to make it easy to follow in 4 simple steps.

Connection Strings and Configuration Files, ConnectionStringSettingsCollection ConnectionStrings { get; } The following example shows how to use a connection string to read data from a database. The previous example assumes your project has an App.config as shown below. Connection string for excel sheet in app.config file unable to add/update connection string in app.config Encrypting or Removing Connection String from APP.config

Also check that you've included the System.Configuration dll under your references. Without it, you won't have access to the ConfigurationManager class in the System.Configuration namespace.

ConfigurationManager.ConnectionStrings Property (System , Net by reading the value of the Connection String from the ConnectionStrings section of the App.Config. In Windows applications, Class Library or Console  Here Mudassar Ahmed Khan has explained how to use Connection String from App.Config file using C# and VB.Net by reading the value of the Connection String from the ConnectionStrings section of the App.Config. In Windows applications, Class Library or Console applications one has to reference the System.Configuration Assembly in order to read Connection String value from the App.Config file

string str = Properties.Settings.Default.myConnectionString; 

Read (Get) Connection String from App.Config file using C# and VB , The examples is applicable also to .NET applications using app.config. Configuration to get access to the ConfigurationManager class. // C# using System. Then According to your app.config get the connection string as follow. string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString(); That's it now you have your connection string in your hand and you can use it.

First Add a reference of System.Configuration to your page.

using System.Configuration;

Then According to your app.config get the connection string as follow.

string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();

That's it now you have your connection string in your hand and you can use it.

Store Connection String in Web.config, This document explains how to configure a connection string at runtime for a Windows application using C#. When retrieving connection strings by provider name from the app.config file in a Windows application, the connection strings in machine.config get loaded first, and then the entries from app.config. Adding clear immediately after the connectionStrings element removes all inherited references from the data structure in memory, so that only the connection strings defined in the local app.config file are considered.

Configuring Connection String in App.Config File During Runtime , How to get the connection string from the app.config file in a C# console application. Get connection string from App.config. 0. ConnectionString with external file for SqlConnection. Hot Network Questions Is the word "psithurism" really used in English

Console C# get database connection string from app.config, Use the database connection from your app's config file (both win and web), rather than always having to To get this to work I needed to explicitly add "​System. Console C# get database connection string from app.config ◎ Published: Nov 28, 2010 When writing a console application using C# /.NET and you want to connect to a database, the connection string is typically stored in the app.config file. Below is an example of a web.config which is the same an app.config file used in C#.Net.

How to Use ConnectionString from App config file in Visual Studio ,

Comments
  • Where did you put that App.config? Project of application you're running or maybe some dll? You need first
  • Add a reference to System.Configuration.dll, and you should be able to use the System.Configuration.ConfigurationManager.
  • Your connection string has a typo. You need a space between "Integrated" and "Security"
  • @OnurOmer - question has been updated to include the space ("Integrated Security" instead of "IntegratedSecurity")
  • I know that this is a C# question, but since this is the top of the google search results, to do this in VB, it's System.Configuration.ConfigurationManager.ConnectionStrings("Test").ConnectionString for those of us who have to maintain VB code
  • Love this answer. By default in my version of VS (VS2012 Ultimate) this library is not included, but using System.Configuration still works
  • I really HAD TO add the reference or the using was not underlined but still ConfigurationManager unknown. Thanks!
  • What if app.config is added as a link to a project ?
  • This will only work if your connection string is an app setting, it will not work if to retrieve <connectionStrings> elements from app.config (which is what the OP is asking for!).
  • I did the same as u said but same null reference error I am getting.