G
Guest
A friend handed me a copy of MS Press's 'Programming ADO.Net 2.0'. There is
a bit of code on pg 99 regarding config files. I have been trying to write a
demo program based on this for my own understanding. The purpose (I thought)
was quite simple: to create an editing screen for config strings, to read,
write, and update them. Unfortunately, while I can add strings, I can't seem
to update them. I keep getting 'configuration is read only' messages.
Here's the code for my Save button.
Dim setting As ConnectionStringSettings
Dim config As Configuration
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Try
setting = ConfigurationManager.ConnectionStrings(txtName.Text)
If Not setting Is Nothing Then
' An attempt to get around the read-only problem.
ConfigurationManager.ConnectionStrings.Remove(txtName.Text)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
End If
setting = New ConnectionStringSettings()
setting.Name = txtName.Text
setting.ConnectionString = txtContent.Text
config.ConnectionStrings.ConnectionStrings.Add(setting)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)
Catch ex As Exception
MsgBox(ex.Message)
End Try
a bit of code on pg 99 regarding config files. I have been trying to write a
demo program based on this for my own understanding. The purpose (I thought)
was quite simple: to create an editing screen for config strings, to read,
write, and update them. Unfortunately, while I can add strings, I can't seem
to update them. I keep getting 'configuration is read only' messages.
Here's the code for my Save button.
Dim setting As ConnectionStringSettings
Dim config As Configuration
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Try
setting = ConfigurationManager.ConnectionStrings(txtName.Text)
If Not setting Is Nothing Then
' An attempt to get around the read-only problem.
ConfigurationManager.ConnectionStrings.Remove(txtName.Text)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)
config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
End If
setting = New ConnectionStringSettings()
setting.Name = txtName.Text
setting.ConnectionString = txtContent.Text
config.ConnectionStrings.ConnectionStrings.Add(setting)
ConfigurationManager.RefreshSection("connectionStrings")
config.Save(System.Configuration.ConfigurationSaveMode.Full)
Catch ex As Exception
MsgBox(ex.Message)
End Try