Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 21st, 2008, 08:27 AM
Registered User
Join Date: Jul 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default ConnectionString setting at runtime

Hi folks,

I am trying to dynamically change (at runtime) the ConnectionString section settings in the web.config file.

I have tried the following coding, but I get an error "The configuration is read only", which happens at the .Clear stage, and if I comment this line out, it then happens at the .Add stage.

Dim conn as New ConnectionStringSettings
            Dim connString as String
            connString = "Data Source=mySqlservername;Initial Catalog=mysqldatabasename;Trusted_Connection=True"
            conn.ConnectionString = connString
            conn.Name = "LocalSqlServer"
            conn.ProviderName = "System.Data.SqlClient"
        ConfigurationManager.RefreshSection("connectionStr ings")

I want to be able to allow the users to change the back-end database (SQL Server) at runtime. All the databases will be structurally identical, but they will contain different data.

Any ideas?

Kind regards


Old July 21st, 2008, 08:36 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You might be better off to load the connection string into some runtime variable and use that as the source for your connections. Load it from the web.config as the default, change it if necessary based on a user choice and store it somewhere it can be used by all database connection objects.

Most of my sites use a base page class that all pages derive from for the purpose of providing access to commonly used values and utilities. That's where I'd put it personally, but you may not use that model.

Old July 22nd, 2008, 03:30 AM
Registered User
Join Date: Jul 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

Hi Peter,

Thanks for the suggestion. However, my understanding is that ASP controls such as the GridView pick up their ConnectionString parameters from the web.config file e.g.

<asp:GridView ..... DataSourceID="SqldataSource1" .... />
<asp:SqlDataSource ID="SqldataSource1" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" .... />

If this is the case, is it possible to pass a variable to the ConnectionString parameter of the <asp:SqlDataSource..../> tag?

I know I can put something like:-

<asp:SqlDataSource ....ConnectionString="Data Source=mySQLServer;Initial Catalog=mySQLDatabase;Integrated Security=True" ..... />

but how do I put a variable into the Initial Catalog section of this parameter?

If you have any further thoughts or ideas, I would be very grateful.

Kind regards,


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection String setting at runtime TwoTrees BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 3 July 2nd, 2010 11:41 AM
where to put my connectionstring pascali70 C# 10 June 22nd, 2007 05:09 AM
Need help! Setting PK value at runtime! Darrell86 SQL Server 2005 6 November 20th, 2006 01:33 AM
Setting Runtime DB connection from JSP ssivakumar76 BOOK: Beginning Java 2, JDK 5 Edition 0 June 16th, 2006 03:07 AM
setting DBNull to a string in a dataset at runtime texasraven Classic ASP Professional 2 September 3rd, 2003 04:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.