Read connection string from web.config

C#.Netasp.netWeb ConfigConnection String

C# Problem Overview


How can I read a connection string from a web.config file into a public class contained within a class library?

I've tried:

WebConfigurationManager

ConfigurationManager

But these classes are not recognized within my class library.

C# Solutions


Solution 1 - C#

You need to add a reference to System.Configuration and then use:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

Solution 2 - C#

Add System.Configuration as a reference.

For some bizarre reason it's not included by default.

Solution 3 - C#

C#

// Add a using directive at the top of your code file    
using System.Configuration;

// Within the code body set your variable    
string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

VB

' Add an Imports statement at the top of your code file    
Imports System.Configuration

' Within the code body set your variable    
Dim cs as String = ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString

Solution 4 - C#

Add System.Configuration as a reference then:

 using System.Configuration;

 ...

 string conn = 
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;

Solution 5 - C#

I guess you need to add a reference to the System.Configuration assembly if that have not already been added.

Also, you may need to insert the following line at the top of your code file:

using System.Configuration;

Solution 6 - C#

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;  

C#

string constring = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring))

BELOW WEB.CONFIG FILE CODE

<connectionStrings>
    <add name="ABCD" connectionString="Data Source=DESKTOP-SU3NKUU\MSSQLSERVER2016;Initial Catalog=TESTKISWRMIP;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

In the above Code ABCD is the Connection Name

Solution 7 - C#

In VB : This should work

ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString

In C# it would be (as per comment of Ala)

ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString

Solution 8 - C#

You have to invoke this class on the top of your page or class :

using System.Configuration;

Then you can use this Method that returns the connection string to be ready to passed to the sqlconnection object to continue your work as follows:

    private string ReturnConnectionString()
    {
       // Put the name the Sqlconnection from WebConfig..
        return ConfigurationManager.ConnectionStrings["DBWebConfigString"].ConnectionString;
    }

Just to make a clear clarification this is the value in the web Config:

  <add name="DBWebConfigString" connectionString="....." />   </connectionStrings>

Solution 9 - C#

using System.Configuration;


string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();

Solution 10 - C#

using System.Configuration;


string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();

Remember don't Use ConnectionStrings[index] because you might of Global machine Config and Portability

Solution 11 - C#

First add this:

using System.Configuration;

Solution 12 - C#

Everybody seems to be suggesting that adding

using System.Configuration;

which is true.

But might I suggest that you think about installing ReSharper's Visual Studio extension?

With it installed, instead of seeing an error that a class isn't defined, you'll see a prompt that tells you which assembly it is in, asking you if you want it to add the needed using statement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchamaraView Question on Stackoverflow
Solution 1 - C#Muhammad AkhtarView Answer on Stackoverflow
Solution 2 - C#peteisaceView Answer on Stackoverflow
Solution 3 - C#MDMView Answer on Stackoverflow
Solution 4 - C#nirmusView Answer on Stackoverflow
Solution 5 - C#Akram ShahdaView Answer on Stackoverflow
Solution 6 - C#subramanya46View Answer on Stackoverflow
Solution 7 - C#AlaaView Answer on Stackoverflow
Solution 8 - C#Ahmed ElbattView Answer on Stackoverflow
Solution 9 - C#Saravanan GView Answer on Stackoverflow
Solution 10 - C#cyberspiritwebsolutions ugoView Answer on Stackoverflow
Solution 11 - C#AliView Answer on Stackoverflow
Solution 12 - C#Jeff DegeView Answer on Stackoverflow