Page 1 of 1

Connection String change at runtime

Posted: Mon 18 Oct 2010 16:50
by sc2269
I need to dynamically generate my connection string at runtime since the database that is used is dependent on who is logged into the system. I would like to generate the connection string in its own class... however I have not been able to get any of the Devart design tools to work unless they reference a connection string in web.config or system settings. Is there a way to get the design tools to work and reference a connection string returned in a class/method?

For example I can manually change the generated code to the following.... this works for my executed code but nothing works in Visual Studio at design time (making development more challenging):

Dim conString As clsConnectionString = New clsConnectionString
Me.MySqlConnection1.ConnectionString = conString.GetConnectString(HttpContext.Current.Session("domain"))
Me.MySqlConnection1.Name = "MySqlConnection1"
Me.MySqlConnection1.Owner = Me

Maybe someone has suggestions on another way to do this? Or how to make this work?

Posted: Tue 19 Oct 2010 09:53
by fritsr
Just a suggestion. Can't you just set a connectionstring at designtime that you can use to develop. And than change that connectionstring to whatever you want at runtime ?

Posted: Tue 19 Oct 2010 14:58
by StanislavK
As I can understand, you wish to use at design-time a connection string generated at run-time. Am I correct? This is impossible, at least because your run-time objects (e.g., HttpContext) do not exist before your application is started.

However, you can use the approach suggested by fritsr: perform the design-time configuration with a hardcoded connection string, and then use connection strings returned at run-time in your code.