How can I change the connection string for a Membership Provider in the Global.asax file?

A

Alias

Hi -

I have a site that runs on a development and production server. I'd like to
programmatically change the connection string the provider uses in the
web.config file and have the provider use the updated string. I am able to
successfully change the string using this code in the Application_Start
method of the Global.asax file:

Dim csNew As ConnectionStringSettings

Dim config As Configuration =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath)

csNew = New
ConnectionStringSettings(config.ConnectionStrings.ConnectionStrings("conn").Name,
"cstring",
config.ConnectionStrings.ConnectionStrings("SPIncConn").ProviderName)
If (config.ConnectionStrings.ConnectionStrings("conn").ConnectionString <>
"correct cstring") Then
config.ConnectionStrings.ConnectionStrings.Remove("conn")
config.ConnectionStrings.ConnectionStrings.Add(csNew)
config.Save()
End If

After this runs I can open the web.config and see the correct
connectionString entry is in it. So that is working. However when I try to
log in the Membership system in ASP.Net is still hooked up to the old
database somehow. From what I've read I figured maybe the config.Save()
method would cause an app restart which would in turn cause the Membership
stuff to sync with the new database. The connectionStringName propery in my
provider doesn't need to change because when I alter it I'm using the same
name.

I think this is being cached somewhere. The weirdest part is if I go into
IIS, stop the app, then restart it, it is still hooked up to the wrong DB
even though I can physically open the web.config and see that it is now
holding the correct string for that server. However if I physically reboot
the whole server it then notices the new string and works just fine.

Does anyone know where this would be cached or stored outside of the
application itself? When I run locally through VS.Net 2005 this works like
a charm. It isn't a permission issue with modifying the web.config because
I can see that the file is being modified successfully on the server.
Thanks for any insight!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top