Using ConnectionStrings Section in ASP.NET 2.0

ASP.NET 2.0 introduces a new section to the web.config file called ConnectionStrings. Some previous projects in ASP.NET 1.1 created this section so you may be familiar with it. This section allows users to add connection strings in their web.config for different data sources. An example of one using SqlExpress 2005 is shown below:

    <connectionStrings>
        <add name="MainConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\YourDB.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>


When referencing this connection string in a built-in DataSource control, you can choose the connection from within the control's wizard or manually modify the ConnectionString and ProviderName properties of the DataSource control. An example is shown below:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        SelectCommand="select * from [Users]"
        ConnectionString="<%$ ConnectionStrings:MainConnectionString %>"
        ProviderName="<%$ ConnectionStrings:MainConnectionString.ProviderName %>">
    </asp:SqlDataSource>


You can also access the connection string in a code file in the App_Code folder by using the ConfigurationManager class like this:

    Dim conn as SqlConnection
    conn = New SqlConnection(ConfigurationManager.ConnectionStrings("MainConnectionString").ConnectionString)


This makes accessing your connection strings and managing them much easier.
Published Thursday, May 12, 2005 3:14 PM by Jason N. Gaylord

Comments

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, May 12, 2005 3:43 PM by Hannes Preishuber
In B2 you should WebConfigurationManager

# re: Using ConnectionStrings Section in ASP.NET 2.0

Monday, May 16, 2005 9:38 PM by YeYan
RePost:
http://www.yeyan.cn/Programming/ConnectionStringsNET.aspx

# Using ConnectionStrings in ASP.NET 2.0

Monday, May 23, 2005 12:23 PM by TrackBack
TrackBack From:http://www.cnblogs.com/na57/archive/2005/05/24/161098.html

# re: Using ConnectionStrings Section in ASP.NET 2.0

Wednesday, October 18, 2006 2:06 AM by Cory Foy

It actually should be

ConfigurationManager.ConnectionStrings["blah"]

Or, in other words, the ConnectionStrings should be brackets instead of parens.

Good post though!

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, April 05, 2007 4:43 PM by Joe Dirt

Not in VB, assmonkey

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, May 24, 2007 3:15 PM by Guest

Please stop posting broken code.

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, June 21, 2007 10:29 AM by pinko

it is C# code:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnectionString"].ToString());

# re: Using ConnectionStrings Section in ASP.NET 2.0

Tuesday, July 24, 2007 11:15 AM by Stephen Lowe Watson

All fine as long as you always use the same provider. But you should be able to say

IDbConnection Conn = ConfigurationManager.ConnectionStrings.NewConnection("blah blah");

and get whatever connection type is specified in ProviderName

# re: Using ConnectionStrings Section in ASP.NET 2.0

Sunday, August 05, 2007 1:23 AM by Tsuruda

buy-tramadol--online.info/.../tramadol-sizes-and-shapes.php

# re: Using ConnectionStrings Section in ASP.NET 2.0

Saturday, September 22, 2007 8:13 AM by SD

Is it  possible in separate class Library project to access following Property ? System.Configuration.ConfigurationManager.ConnectionStrings []

I tried for this but it doesn't access above property in class library project but in ASPx page I am getting above property.

So I used traditional method in class library project as

System.Configuration.ConfigurationSettings.AppSettings["DbChangeControlCon"]...

# re: Using ConnectionStrings Section in ASP.NET 2.0

Saturday, September 22, 2007 8:35 AM by SD

Is it  possible in separate class Library project to access following Property ? System.Configuration.ConfigurationManager.ConnectionStrings []

I tried for this but it doesn't access above property in class library project but in ASPx page I am getting above property.

So I used traditional method in class library project as

System.Configuration.ConfigurationSettings.AppSettings["DbChangeControlCon"]...

# re: Using ConnectionStrings Section in ASP.NET 2.0

Thursday, November 29, 2007 6:24 PM by PRESTON,Play the race card

Play the race card

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, November 30, 2007 11:22 AM by MICKEY,We are a grandmother

We are a grandmother

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, February 01, 2008 12:33 AM by mehregan

Thanks

# Lexapro.

Monday, July 14, 2008 3:18 PM by Lexapro versus zoloft.

What is lexapro. Lexapro.

# Buspar and weight gain.

Thursday, July 31, 2008 1:32 AM by Buspar.

Buspar online cheaper. Buspar. Bristol-meyer squibb buspar. Buspar alcohol. Buspar and modafinil together.

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, November 28, 2008 7:36 AM by Olgunka-fh

<a href= http://fasster.angelfire.com >baltimore and convention center and headquarters</a> <a href= http://gertui.angelfire.com >nasdaq 100 tennis tournament</a>

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, November 28, 2008 7:58 PM by Olgunka-gf

<a href= http://chkola.angelfire.com >avlastkey</a> <a href= http://bustersw.angelfire.com >how to start a strawberry patch in alabama</a>

# re: Using ConnectionStrings Section in ASP.NET 2.0

Tuesday, December 02, 2008 11:57 PM by Asina

<a href= bestpre.com ></a>

# re: Using ConnectionStrings Section in ASP.NET 2.0

Friday, December 26, 2008 8:24 AM by balabo3_rx

<a href= membres.lycos.fr/maffals >genetic disorters</a>

# re: Using ConnectionStrings Section in ASP.NET 2.0

Sunday, March 01, 2009 12:24 AM by balabo3_is

<a href= adultspeeddatingfinder.com >chat online</a>

# re: Using ConnectionStrings Section in ASP.NET 2.0

Monday, May 11, 2009 2:42 AM by Ali

How about the connection string with an AJAX enabled website???

I have not seen any <connectionStrings></connectionStrings>

Pls advice.

# re: Using ConnectionStrings Section in ASP.NET 2.0

Wednesday, June 10, 2009 10:40 PM by gas powered scooters

Help me to find extremely cheap gas powered scooters

www.world66.com/.../gas_powered_scoote

# re: Using ConnectionStrings Section in ASP.NET 2.0

Monday, June 29, 2009 7:02 AM by meganathan

Windows Data Protection application programming interface (DPAPI) protected configuration provider and the Aspnet_regiis.exe tool to encrypt sections of your configuration files. You can use the Aspnet_regiis.exe tool to encrypt sensitive data, such as connection strings, held in the Web.wanted to share about the software testing ebooks information. Many of you intrested to start your carreer in testing. you can get lot of information about the Software Testing from Macrotesting . Any of you guys get useful information about this please share wih me.

All the best for your carrer..

Meganathan

Leave a Comment

(required) 
(required) 
(optional)
(required)