Thabo
Thabo

Reputation: 1554

Issues on dynamically change and switching the connectionString in web Config

I am trying to switch different databases for a web application at Run time.

Senario We have one asp.net web application and different databases for different customers.I am trying to switch particular connection string value from a common database where i am keeping a mapping table for connection string ,particular customer id and password .After the successful lo gin i am piking a connection string from the common database and edit the web.config file connection string section by replacing selected connection string at run time. i am doing this by add following code to login event

                conectionString = cString;
                Configuration openWebConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
                ConnectionStringsSection sections = openWebConfiguration.GetSection("connectionStrings") as ConnectionStringsSection;
                if (sections != null)
                {
                    sections.ConnectionStrings["ConnectionStringName"].ConnectionString = conectionString;
                    ConfigurationManager.RefreshSection("ConnectionStringName");
                    openWebConfiguration.Save();

                }

i am reading above connection string on a page by using ConfigurationManager.problem is the web config file is changing but after calling to another page using Response.Redirect will throw an exception .Exception is "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack "I can realized this is something happen on cross threaded environment.My questions are

Upvotes: 4

Views: 3511

Answers (1)

Thabo
Thabo

Reputation: 1554

I am wondering why this question seems still unanswered.OK i have found some answermy self.It may be wrong but some how they are giving some meaning to me.I assume following are acceptable for my knowledge level.

1)I don't know the exact reason ,but this is something happen because of code is modified while an application running

2)Based on my search WEB Config file is started to read by the application when the IIS server start.So what ever values to be modified inside WEB Config ,require to restart the IIS server to load them in to memory.We can modify the connection string dynamically but still the application will run on the previous connection string.So we need t restart the IIS to load newer one.

Note:Modify a existing connection string is different than add a new connection string to a WEB Config.

3)I have used a common data Base where i have authentication details for different different connection strings for Several database.WeB config has the connection string for above master database.If an user gives his authentication detail it will select his connection string and load it as new connection string .So the remaining process will be based on that connection string.

Any new arguments for above answers are highly appreciable.I need corrections from other developers because i am very eager to learn.

Upvotes: 1

Related Questions