Dynamicaly updating a .config file

  • Thread starter Thread starter Lilian
  • Start date Start date
L

Lilian

Hi,

VS2K8, .NET 2.0

In a standard SQL Server client application the connection string is stored
in my .config file.

I would like the user to be able to switch from one database to another
without exiting the application.

When updating the .config using ConfigurationManager class, and Save()
method I can achieve changes in the file.
The problem is that once Save() has been called, the connection string value
obtained by reading through ConfigurationManager still contains the old value.

Any idea ?
 
Lilian said:
I would like the user to be able to switch from one database to another
without exiting the application.

When updating the .config using ConfigurationManager class, and Save()
method I can achieve changes in the file.
The problem is that once Save() has been called, the connection string
value
obtained by reading through ConfigurationManager still contains the old
value.

You could load the connection string into a global variable, and then all
connections read from the variable instead of the config. Then, the user can
change the default connection by changing the variable?
 
Leon Mayne said:
You could load the connection string into a global variable, and then all
connections read from the variable instead of the config. Then, the user can
change the default connection by changing the variable?

I thought of this kind of workaround but at this step the application is
also using Wizard built code (such as Dataset) calling the connection string
directly from the .config file.

In addition I prefer not to store the same information in two places...

Thank you anyway for the advise.
 
Thank you Wil.

I already found this info indeed and tried it. It works fine for my code
when trying to retrieve the modified parameter value.

The problem is still with the datasets that seem to be initialized at the
application startup and thus cannot be redirected to the new connection
string value.

Am I right ?
 
I'm not too familiar using DataSets, but I would assume if your user
switches databases then all the data contained in the datasets is no longer
of use and that dataset could be disposed of. If this is the case, can you
dispose of the dataset and re-initialize it after a database change?
Without knowing much about DataSets and your application my guess is this
would be the 'cleanest' way of accomplishing the task.

HTH - Wil
 
Thank you again. I am a beginer too with datasets (and the whole .NET
framsework in fact...) but I think I managed to find a solution :
- Once you define a dataset using the IDE wizard, the connection string is
"hard coded" in the dataset class definitoin.
- You can however override this setting by using the table adapter's
Connection member in your "Load" form method.

Ie :
// GetConnection() returns a SqlConnection instance in my project
this.Customer_TableAdapter.Connection = Program.Database.GetConnection();
this.Customer_TableAdapter.Fill(this.MyDataSet.Customer);

This way any time the form is opened it will use the current connection
string.

The next step is then to manage the case where the user changes the database
connection while some forms are still opened from the previous database...

Thank you all for your help.
 
Sounds like you figured it out on your own, that's great! The hard coding of
the connection string on your dataset is the exact reason why I have avoided
use of DataSets and most wizards. I like to know what my code is doing vs.
having some design time tool create code for me.

Take care - Wil
 
Back
Top