Coder Perfect

Obtain the connection string from the App.config file.

Problem

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

And here’s what my App.config looks like:

<?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>

However, when my project runs, I get the following error:

Asked by Moham ad Jafari

Solution #1

Isn’t it possible for you to simply perform the following:

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

A reference to System is also required in your assembly. Configuration.dll

Answered by Duffp

Solution #2

Because this is a frequently asked subject, I’ve produced some screen pictures from Visual Studio to help you follow along in four simple stages.

Answered by Fredrick Gauss

Solution #3

string str = Properties.Settings.Default.myConnectionString; 

Answered by gjijo

Solution #4

Make sure you’ve included the System as well. Configuration dll is located in your references folder. You won’t be able to use the ConfigurationManager class in the System without it. Namespace for configuration.

Answered by Carl Heinrich Hancke

Solution #5

To begin, add a System.Configuration reference to your page.

using System.Configuration;

Then, according to your app.config, receive the following connection string.

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

That’s all there is to it; you now have your connection string in your hand and can utilize it.

Answered by Tapan kumar

Post is based on https://stackoverflow.com/questions/6536715/get-connection-string-from-app-config