 |
|
 |
Do you have an example of how to call this from c#?
Joe
|
|
|
|
 |
|
 |
Just call SetUserOverride(...).
For example:
xxx.Properties.Settings.Default.SetUserOverride("constring1",_NewConnectionString);
where:
1. xxx is settings class namespace,
2. "constring1" is your old connection string name,
3. _NewConnectionString is your new connection string.
If you want to persist changes, call Save().
|
|
|
|
 |
|
 |
Thanks for confirming that. I've attempted that method, but cannot see the method via intellitype.
|
|
|
|
 |
|
 |
Make sure your partial Settings code is in the correct namespace.
|
|
|
|
 |
|
 |
Thanks for the information. The correct namespace was:
namespace myapplicationname.properties{
}
|
|
|
|
 |
|
 |
Thanks so much. You explained the situation and gave a terrific solution.
|
|
|
|
 |
|
 |
You're welcome. I accept donations. If you have a small country you no longer want, I'll be glad to accept it.
|
|
|
|
 |
|
 |
Thank You
I think
My.Settings.Item("ConnectionStringName") = NewConnectionString
will make me able to change the connection string with out any classes
Am I right?
George Batres
modified on Thursday, December 4, 2008 9:47 AM
|
|
|
|
 |
|
 |
If you are using a connectionstring which is automatically created by the visual studio, that connection string is an application scoped connection string an is read-only. The command you offer will change the string only in-memory an will not save the connection string. So next time you run the application, the change will be lost unless you have found some other way to recall the change string. Also, since tableadapters reference only the name of the connection string explicitly, then if you have table adapters added to the project when in forms design mode and you load your updated connection strings outside of the 'settings' or 'my.application' classes there is a possibility that a table adapter gets initialized prior to your loadding the new connection string and that will cause the table adapter to use the wrong string.
|
|
|
|
 |
|
 |
i do what u ask
my connection is natconnectionstring scop applecation ok
i have one connection string natconnectionstring
i put in the overide like u tell in the place of connection1
i remove connection2
i use the class as u tell me
but nothing
help me please
explan exactly what we do or please
attach simple project that do this class
pleassssssssssss
|
|
|
|
 |
|
 |
It is probably step 3 that you have not understood. You need to check to see the name of the connection string you used when you created your datasets. This is the name that your strongly types dataset and table adapter expect to use. And it is that name that must be used instead of connectionString1 or connectionString2 in the examples above.
This example ONLY works with strongly typed database access. It could be adapted to use other datasets, but that is project left to the reader to accomplish on their own.
|
|
|
|
 |
|
 |
Hi. Thank you for share this kind of information.
I'm a little confuse with the application that I try to use your code. Maybe you can help me. My application has 2 layer. One is a library where I create all my datasets with all the tables (this create a dll). I use only one connection string. The other layer is where reside the forms and all the logic.
I create a module folowing your istructions but, when I try to change a connection string I get an error. Something like "the parameter connectionstring1 is not valid". I think this is because my connection string is on the other layer.
Do you think there is a way to use your solution in my case? I create a solution using a instruction like Me.UsersTableAdapter1.Connection.ConnectionString = "new connection string" but this is to ugle".
Any help will be great.
Again, thanks for share your code.
Rogerio
|
|
|
|
 |
|
 |
I have not tried to run this as a separate library. I am pretty sure what you are seeing is what is expected since this relies pretty heavily on the VB application framework that only applies the the main forms assembly.
|
|
|
|
 |
|
 |
Ok so I followed the procedure and it seems to work.
After I run the last bit of code on the changing procedure
-> My.Settings.SetUserOverride("MyConnectionString", MyText)
I then call for the current setting allocated to the MyConnectionString as a string
and put it in a message box. It seems to be what I want. But, then using the END command
to close the app, I dont see changes made to the string in the Application Settings tab
under MyConnectionString. It is exactly what it was before I ran the code.
What am I doing wrong? I changed the text in userOverride_SettingsSaving from
Me(appProperty) = Me(userProperty) to Me(userProperty) = Me(appProperty)
But this does not seem to show me the new string in my settings page either.
The MyConnectionString is the application scope connection string.
Please help?
|
|
|
|
 |
|
|
 |
|
 |
First - My English is very bad
I want only change in run-time my connection string (in my.settings, type connection string, scope application)
I want change in run-time, and if i close my app the settings are save.
I install my app in my client and i want change the connection string forever.
Thanks
|
|
|
|
 |
|
 |
When you want to change the connection string in run-time and also have the change saved when the program exits, you do this My.Settings.SetUserOverride("ConnectionString1", "My new string").
When you want to change the connection string in run-time but do not want the change saved when the program exits, you do this My.Settings.Item("ConnectionString1") = "My new string".
And if you use table adapters, then after you change a connection string in run-time you must also re-created the table adapter object in run-time (MyTableTableAdapter = New MyDataSetTableAdapters.MyTableTableAdapter) since the original table adapter will have the old connection string.
|
|
|
|
 |
|
 |
After downloading the code, I discovered that it differs from the code displayed in the article and I wondered if you can tell me which is correct.
In the article, the private sub userOverride_SettingsSaving has this line of code:
Me(userProperty) = Me(appProperty)
In the downloaded code, this subroutine has this line of code:
Me(appProperty) = Me(userProperty)
Which one is correct? I changed the downloaded code to match the article's code and didn't notice any difference in execution...both ways seem to work fine.
Also, I'm having a difficult time understanding when and where the userOverride_SettingsLoaded and userOverride_SettingsSaving subs fire. Can you explain?
Thanks,
dlcarp
|
|
|
|
 |
|
 |
SettingSaving has the line "Me(userProperty) = Me(appProperty)".
SettingsLoaded has the line "Me(appProperty) = Me(userProperty)".
The need to be like this and opposite from each other.
MySettings.SettingsLoaded is fired upon the first reference to My.Settings. MySettings.SettingsSaving is fired after the last form is closed and after My.Application.Shutdown.
|
|
|
|
 |
|
 |
Hey, mjmeans..thanks for the feedback!
|
|
|
|
 |
|
 |
Hi, im trying to change my connection string on runtime. I tried this code but its not working for me. I have a DBConnectionString with application scope and I made DBConnectionStringUser with User scope.
The value for DBConnectionString is "Data Source=SERVER1;Initial Catalog=DB1;Integrated Security=True". I have a user login form where user enters username and password and a combobox where he choose a database(DB1,DB2,DB3). This is what I put in cmbbox_selectedindexchanged:
sqlcon = "Server=SERVER1;DataBase=" & cmbbox.selectedtext & ";Integrated Security=SSPI;Connect Timeout=1"
My.Settings.SetUserOverride("DBConnectionString", "Data source= SERVER1;Initial Catalog=" & cmbbox.selectedtext & ";Integrated Security=true")
The problem is that it doesnt use the conn.string i put in SetUserOverride but it uses the value I set for DBConnectionStringUser in project properties.
Am I doing something wrong?
|
|
|
|
 |
|
 |
The first thing that comes to mind is that you might be seeing this in the IDE. Since each time you build your project, a new set of .config files are copied to the bin folder. So what you are experiencing might be related to your user overrides being overwritten by the IDE when you recompile.
The second possibility is that you might not have checked the box on the project properties to save the settings on exit.
So, try setting a couple of break points in SettingsLoaded and SettingsSaved and make sure that they are being called by the Windows Application Framework.
Next try running your app directly from windows explorer, browse to and run the exe file in the bin directory. then check the .config file and see it is saved the changes.
A third possibility that might cause this problem is if you are using clickonce deployment. The framework needs an explicit call to My.Settings.Upgrade() whenever a new version using clickonce is run, otherwise it looses the user settings from the previous version. The problem is that the program has no way of knowing whenther it is a newer version, and you dont want to repeatedly call Upgade(); so the a quick little workaround is to add Boolean user setting called "CallUpgrade" and set it to True. Then very early in your startup code run this little snippet:
If My.Settings.CallUpgrade Then
My.Settings.Upgrade()
My.Settings.CallUpgrade = False
End If
|
|
|
|
 |
|
 |
It's working now. The problem was in calling the SetUserOverride after making the connection. It should go before making a connection.
Thank you!
modified on Thursday, June 19, 2008 9:24 AM
|
|
|
|
 |
|
 |
Well done Code-project, I find this article remarkable for two reasons:
1. That it is so clearly written, easy to implement, and works first time. Thank you very much.
2. That it is required at all, took me so long to find, and that Visual Studio was so poorly signposted to get here, and frankly that Microsoft requires such a workaround for the simple task of implementing my application on a different server with a different name to the development server. How opaque and poorly documented can a major development environment be, and how unable to accommodate such a simple and regular development requirement? Blown away.
So Remarkable: well done code-project, and thanks so much for your article.
Regards,
Pete
Web and Software Development in Cornwall - United Kingdom
|
|
|
|
 |
|
 |
You're welcome. And thank you for the kind words. Your second reason is exactly the reason I wrote this. My own product is developed to use SQL server and strongly typed datasets as well. All the other "workarounds" I found were so obtuse and difficult to maintain that I decided a better solution was needed.
When I thought of this idea, I decided it was too good to keep to myself; maybe even good enough to actually make some money on. But it was such a tiny piece of code that I really wouldn't feel right charging a consultancy fee for it. Maybe I can convice codeproject to kick me some of the ad revenue they generated from my article? Yeah... like that could ever happen. NOT.
Mark
|
|
|
|
 |