ASP.NET Modify connection string at application start page

I'm going to have several sub-domains point to an website application.  After detecting the originating sub-domain (a separate question), I need to use that sub-domain name to modify a site connectionstring to point to a different database (one database per sub-domain.)

This has to happen in the Page_Load method of a login page that uses the aspnet_Membership login control.  That is, I need to reset the connection string to point to the correct DB before the login control can operate.

I've seen a couple suggested methods for modifying connection strings however, I'm not clear on which one to use.

I tried the code below, however, the second response.write, which should show the result, shows no change.  However, maybe I'm re-reading the original unchanged value.

 Dim wrkNewConnStr As String
            wrkNewConnStr = "Data Source=localhost; Initial Catalog=test01;"
            wrkNewConnStr = wrkNewConnStr & " User ID=testuser1; Password=testuserpw1"

            Dim config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")

            'Dim config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
            Dim connectionStringsSection = DirectCast(config.GetSection("connectionStrings"), ConnectionStringsSection)

            Response.Write("1 - " & connectionStringsSection.ConnectionStrings("TestDB1ConnectionString").ConnectionString)

            Dim wrkOrigConnStr As String
            wrkOrigConnStr = connectionStringsSection.ConnectionStrings("TestDB1ConnectionString").ConnectionString
            wrkOrigConnStr = wrkNewConnStr

            Response.Write("2 - " & connectionStringsSection.ConnectionStrings("TestDB1ConnectionString").ConnectionString)

Open in new window

It seems like any such modification to a connection string would need to take place in memory, not on disk, since many users could be accessing the main site from several sub-domains.  However, I would appreciate pointers to something that spells that out explicitly, so I can understand it better.

So, questions:
>  How to dynamically change a connection string at the initial load of a membership login page
>  How does that only affect memory and not disk

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Suppose sub-domains: and

Create multiple named connection strings in your web.config
<add name="dom1" connectionString="server=localhost;database=myDb1;uid=myUser1;password=myPass1;" />
<add name="dom2" connectionString="server=localhost;database=myDb2;uid=myUser2;password=myPass2;" />

Open in new window

Store the result in a session variable
Internally, map the domain to a mapped connection string (domain name could even be name of connection string in web.config for example). You can do this in the AcquireRequestState in Global.asax. Session is available in this event and this event also fires for each request:
void Application_AcquireRequestState(object sender, EventArgs e)
     //Session is Available here
     HttpContext context = HttpContext.Current;
          context.Session["ConnectionStringName"] = HttpContext.Current.Request.Url.AbsoluteUri.Host.Split('.')[0];

Open in new window

Then, whenever you need to access the database, you can use:

Open in new window

which reads the connection string from web.config.

A nice way to work with session is presented here: (then you can avoid accessing Session["var"] directly.
codequestAuthor Commented:
Thanks for the input.  I can see that that is a way to access the various connection strings.  

This application is exclusively and extensively using typed datasets for the database i/o layer.

The remaining part of the puzzle is how to apply the selected configuration string to the dataset(s)  at runtime.

Any suggestions on that?
This MSDN blog indicates how you can change the connection string at runtime:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codequestAuthor Commented:
@MlandaT:  Thanks for the input.  I modified the code in the link and got it to work.  So, that's good.
Unfortunately, there are additional complications, which I'm documenting here for my sake as well as in hope that there might be an answer.

I have a web /site/ project instead of a web /application/ project, so there is no project file and no project settings to modify.
(link says web site projects have no project files)

So, I would need to modify the table adapters each time they are instantiated.  That looks to be on the order of 200 times.  Then i would have to modify all the object data sources, which is many times.  Heading for an alpha/demo release as soon as possible, that's just not in the budget.

I'm going to mark your answers as solutions, though I'd like to leave this out there for a few days and see if it draws any further comments.

It's good to get this resolved in any case.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.