简体   繁体   中英

Error in Dynamic ConnnectionString in Web.config

I want to change Initial catalog in web.config according to value of dropdown list at the time of login and other things will be remain same like user id, data source, password etc.

It's working fine for me but as We all know "name" attribute can not be same in connectionstring. So I am changing the name attribe according to each connectionString.

But the main problem is that I have defined Connection in every page inside the class like this..

SqlConnection cn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connect"].ConnectionString);

So the "name" attribe should be dynamic according to connectionstring in web.config. So I tried to use Session Variable in the place of "name" attribute in each page like this....

SqlConnection cn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings[Session["ConnectionName"]].ConnectionString);

But Unfortunately Session does not work inside class. So what is the solution ?

In this case you can have a master database where you can maintain all the connections/ core part of your project based on your constraints (like year, etc.,).

Now you can pull the connection string from the master database. There are many other scenarios to achieve this but based on your requirement creating a master database is a better approach.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM