Using ConnectionStrings Section in ASP.NET 2.0

Posted by: Jason N. Gaylords Blog, on 12 May 2005 | View original | Bookmarked: 0 time(s)

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.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1174 | Hits: 134

Similar Posts

  • Clean Web.Config Files (VS 2010 and .NET 4.0 Series) more
  • XAML By FARR: Resource Dictionaries Vs User Control Resources more
  • Sending ELMAH Errors Via GMail more
  • ORM Release History : Q1 2009 SP1 (version 2009.1.405.1) more
  • TemplatePanel (aka MasterPage) for Silverlight more
  • skmExpressionBuilders - A Suite of Custom Expression Builder Classes more
  • Introduction to HealthVault Development #6 Fixing a mistake more
  • Whats Wrong With This Code? (#21) more
  • Limiting Code Comments Increases Maintainability more
  • Section 508 Compliant Renamed Accessibility Compliant more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD