I am writting an application and I used Wizard to create DataSets which auto-created their own xml code. This code uses the saved in My.Settings Connection String. Being a setting with an unchangable 'Application' scope i cannot change its value at runtime. The problem is that when I install the Application to my customer the Connection String will have to change (maybe more than once). So is there a way to change the Connection String used by these objects at runtime ?
Asked
Active
Viewed 2.8k times
1 Answers
7
Here's how to edit the setting via code:
My.Settings.Item("ConnectionString") = "some connection string"
However, an easier solution would be to just use the app.config
file. When the wizard creates your DataSets it should be adding the connection strings to app.config
, which will get copied to your project's output directory as <assemblyName>.config
. Then your installer could determine the proper connection string and edit the .config
file automatically. Also, your users could edit the config file manually.

rogcg
- 10,451
- 20
- 91
- 133

CoderDennis
- 13,642
- 9
- 69
- 105
-
For the time being I just tried with success the first solution. – paulcheil Feb 03 '12 at 01:14
-
I have some things to ask about the second solution but I should first try out a few things and save some of your time. Thank you Dennis. But why is My.Settings.Item("ConnectionString") = "new conn str" allowed and My.Settings.ConnectionString = "new conn str" not allowed ? Tried them both and the compiler says its a read only property using this syntax. I mean i get the job done but now I answered a question and created another one !! :) – paulcheil Feb 03 '12 at 01:22
-
Your second question is exactly my question. @dennis Can you answer this, mate? – Mohammed Julfikar Ali Mahbub Oct 28 '17 at 21:56
-
1Hey @coderDennis I found that using your method actually do change the value but when we restart the application it resets to its default value again. – Mohammed Julfikar Ali Mahbub Oct 28 '17 at 23:43
-
@MohammadZulfikar that sounds right. This method doesn't change the default value. Using a `.config` file and editing it to contain the new value is the only way I know to have the change picked up after restarting the app. – CoderDennis Oct 29 '17 at 20:35
-
@CoderDennis Oh! Thanks for the information. I wonder why Microsoft didn't allowed this as if the user can change manually then why can't the developer change through application. I might not right in this point but will you help me to know the reason behind this restriction if you know. It will be a great help. And yeah, one more question. If this doesn't change the default .config file then what does it actually change? Which file? Is there any option to get the .config file location programmatically? #SorryForMakingItTooLong – Mohammed Julfikar Ali Mahbub Oct 29 '17 at 22:01
-
1BTW, the `.Item` part is optional: `My.Settings("ConnectionString") =` or just `My.Settings!ConnectionString =` https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/program-structure/special-characters-in-code#member-access-operators – Slai Oct 30 '17 at 14:24
-
@MohammadZulfikar I believe it's only changed in memory of the running application. You'd have to add some code to persist the setting somewhere if that's what you need. – CoderDennis Oct 30 '17 at 15:21
-
@Slai it's been a while since I've been actively working with VB, but I don't remember ever seeing the `!` operator like that. – CoderDennis Oct 30 '17 at 15:22
-
I think it was left over for backwards comparability with old VB6 or VBA code where for example `record!fieldName` can be used to access database fields. Basically, `a!b` is syntactic sugar for `a("b")` – Slai Oct 30 '17 at 15:52
-
That's interesting. I never worked with VB6 or much with VBA, so never saw it there. – CoderDennis Oct 30 '17 at 17:55