|
|
Comments and Discussions
|
|
 |

|
I can use the code to change the connection string correctly, and it seems to persist; however, on startup, none of the DataGridViews, etc., are filled in; nor do they seem to be tied to the underlying data.
I've tried to chase down what's going wrong, but it seems something deeply internal to how VB.net handles DataSets; I expect that on startup something about the connection string now causes an error, interrupting the normal process of connecting to the database.
In tracing (stepping, that is) I see that the first form, which contains a DataGridView of the AccountCodes's table (in the project named Heathcliff),calls in its load method:
Me.AccountCodesTableAdapter.Fill(Me.MainDataSet.AccountCodes)
The Fill causes a call to InitAdapter, which sets up the table field labels, and calls InitConection:
Private Sub InitConnection()
Me._connection = New Global.System.Data.OleDb.OleDbConnection()
Me._connection.ConnectionString = Global.Heathcliff.My.MySettings.Default.MainConnectionString
End Sub
The second line seems to execute most of the way, but then in some way errors out, and the form pops up with no data. It looks like the failure point is inside the Get for the MainConnectionString property of MySettings, specifically the line
Return CType(Me("MainConnectionString"),String)
It's very odd because the statement sort of looks like it completes (I see a step to the End Get, presumably stepped to because of the "Return defaultInstance" just before it, which seems to have a correct value). But any further stepping pops me out of the entire Form Load event.
It's possible I'm missing something obvious, perhaps because I've been looking at it too long. I'm hoping someone else has had a similar experience and resolved it. (I saw the other msg saying "try just taking out the namespace references; worked for me." I tried playing with that, always had compile errors of one form or another.)
|
|
|
|

|
Try setting a breakpoint in userOverride_SettingsLoaded() and stepping through it. You may have a typo in the name of the connection string. And double check the casing of the string name.
|
|
|
|

|
Hi Experts, I exactly followed mjmeans' steps and still get the following error at .Fill line of code: "Format of the initialization string does not conform to specification starting at index 0." Here is my partial code: Dim obAllMyTA As v_AllMyTableAdapter Dim objAllMyDS As AllMyDataSet obAllMyTA = New v_MyTableAdapter My.Settings.SetUserOverride("DevelopmentConnectionString1", "ProductionConnectionString") objAllMyDS = New AllMyDataSet obAllMyTA.Fill(objAllMyDS.v_AllMy) <-- error out on this line I noticed private sub "userOverride_SettingsLoaded" and "userOverride_SettingsSaving" never got called when stepping through. Do I miss any steps? BTW, I did NOT enter any values for "DevelopmentConnectionStringUserOverride" and "ProductionConnectionStringUserOverride" user scoped strings on the project,Properties, Settings area. Please help! Thank you very much! -Joe
|
|
|
|
|

|
Could you show me partial of your code related to this call. And steps that I might have missed except those stated in the Author's notes. Thank you! -Joe
|
|
|
|

|
You have to wonder exactly why microsoft doesn't build it in to VS... Why Not?
|
|
|
|

|
hi,
your code is really what I was looking for. I need to connect a TableAdapter created at design time to several DB schemas, depending on who logs on at runtime.
I am developing on .NET Framework 4, and I am having troubles in using your code. First, if I use "Namespace My", I get the error message "SetUserOverride is not a member of 'MyProject.MySettings'. Deleting that statement, the code is well-compiled, but at runtime it seems that SetUserOverride has no effects. in fact, the TableAdapter.Connection.ConnectionString is empty.
How can I fix that bug? In detail, how can I recognize if step 5 is correct? thanks
|
|
|
|

|
try just taking out the namespace references; worked for me...
Ryan C. Price
Architect
Activiser Ltd
http://www.activiser.com
|
|
|
|

|
I tried playing around by removing the namespace bit, but always got compile errors of one form or another - what exactly do you mean?
|
|
|
|

|
I mean remove the 'Namespace My' and 'End Namespace' statements at the beginning and end of the file.
I actually simplified my whole settings.override.vb file down to this:
Partial Friend NotInheritable Class MySettings
Inherits Global.System.Configuration.ApplicationSettingsBase
Public WriteOnly Property Override(SettingName As String) As String
Set(value As String)
Me(SettingName) = value
End Set
End Property
End Class
and used this to save my connection string:
My.Settings.Override("ConnectionString") = connectionString
Ryan C. Price
Software Engineer
|
|
|
|

|
This saved me so much time. Thank you so much for this post!
|
|
|
|
|

|
Thanks so much for this. It was sooo easy to follow even a caveman could do it.
|
|
|
|

|
I can only express my gratitude with ridiculous amounts of money, which sadly i don't have right now.. but really thanks!!!
|
|
|
|
|

|
I created my tableadapters in a class library project called ConfigurationControl I think this is exactly what I need but I was wondering how do I use it from another project.
I added the Module called Settings.UserOverride.vb to the ConfigurationControl project but now I need to access it from the Main Data acquisition project. Can I access the ConfigurationControl settings class from the other projects? or do I create another sub routine, say in Module1 of the ConfigurationControl project that then calls the SetUserOverride routine.
<pre>
Module Module1
Public Sub SetNewConnectionString(ByVal settingName as string, ByVal value as String)
My.Settings.SetUserOverride(settingName, value)
End Sub
End Module1
With this, I should be able to call the SetNewConnectionString from the Data Acq. project on startup and change the connection string before using any of the table adapters, right?
Thanks for the help. I appreciate it so much
|
|
|
|

|
Hmmm.
If you make the MySettings class in your class library Public and make sure the class library is in its own namespace, then you should be able to access it from your other projects, such as OtherClassNamespace.My.Settings.ConnectionString = "server=local;etc". Putting it in another namespace will ensure that the hard coded Settings class name will not conflict with another projects namespace.
If you can't put the library in another namespace for design reasons, then you can add a different public class you can call to access the that libraries settings.
|
|
|
|

|
Hi thanks for the response.
This is what I did
After adding the code in this article to the class library (ConfigurationControl), I added a module called Module1 where I created 2 public methods to access the class libraries settings
Module1
Public Sub SetNewConnectionString(ByVal name as string, Byval value as string)
My.Settings.SetUserOverride(name,value)
End Sub
Public Function GetNewConnectionString() as string
Return My.Settings.DatabaseConnection.ToString)
End Function
Now from my other project I call ConfigurationControl.SetNewConnectionString("DatabaseConnection",connString) when change the connection string upon startup.
It seems to be working correctly but I wanted to make sure this was an appropriate solution.
Just out of curiosity, how would i make the settings class of the class library Public as you suggested?
Thanks again for the help
|
|
|
|

|
You just click the icon in solution explorer to show all files, then open up the settings.designer.vb file. Change the class to public in settings.designer and also in the code from my article.
Another way you could do this is to pass the connecction string in the NEW. You would do this by changing your table adapters class to Friend (or internal) which would block its access. But then add a new Public class that inherits the table adapter and set its constructor to New(connstr AS String).
Also, if you're looking for a connection string editor, check out my link at the bottom of the article.
|
|
|
|

|
mjmeans wrote: Another way you could do this is to pass the connecction string in the NEW. You would do this by changing your table adapters class to Friend (or internal) which would block its access. But then add a new Public class that inherits the table adapter and set its constructor to New(connstr AS String).
I originally looked at this option but I didnt want to have to do it for every table adapter everytime.
mjmeans wrote: You just click the icon in solution explorer to show all files, then open up the settings.designer.vb file. Change the class to public in settings.designer and also in the code from my article.
I did this and I am still not able to see the Settings class.
the furthest I can get is otherNameSpace.My.MySettings and then intellisense gives me nothing
|
|
|
|

|
Hi. If you like this project, you might also be insterested in a DataConnectionDialog component I am selling. Please see http://mjmeans.com/dcd.aspx[^] for more informaiton.
|
|
|
|

|
Hi. Thanks by your code.
But, I have a big problem: my solution have 8 projects and I have ConnectionsStrings in all of then.
When we build a publisher/package to deliver our app to customers we need change the app.config to reflect the new enviroment (the server name and passwords).
But if we change the value of app.config only the main project is afected. The others projects don't update to the new value.
How to fix this issue?
|
|
|
|

|
That could be complex or simple, depending on how your distribution is created. There's a number of ways to do this, involving command line switches or modifying the sub-apps to read the main apps app.config file. But anything along this line would have problems if the other executables are already running, since the config file may be locked. Note that reading or writing another apps config file could pose problems in Vista if all the config files are not in the same folder. A better idea is to use the registry to flag when each sub-application needs to retrieve an upadated connection string. You would set up a registry key for each sub-application like HKLM/Manufacturer/Product/SubApp1UpdateConnectionString=NEWSTRING. Each sub-application would check in StartUp() for the existance of this key when they start, and if it is found update their own connection string, call My.Settings.Save() and then remove the registry setting so that that it doesn't get re-applied.
If your other apps are DLL's, then there is an additional problem.... since they may have already created their DataAdapters and would then have an OLD copy of the connection string. In this case, you will probably have to save the connection string and then restart the applcaiton unless you add code to each of your DLL's to recreate the DataAdapters.
|
|
|
|

|
I was desperate 'cause I didn't know how to deploy a project containing a little sql server ce.
I spent 3 days trying to avoid errors given by the application when I tried to show in a form data present within the DB.
THANKS A LOT!
Frankie
|
|
|
|

|
Hi,
Your code is an excellent solution to this problem - and I would like to use it in my A-level coursework for Computing. Would it be possible to e-mail you with regards this?
Thanks very much
Alex
|
|
|
|

|
Sure. Just use the email link at the bottom of this reply and CodeProject will send a private email to me.
|
|
|
|

|
I converted your great article to C# and it worked great. Here's my version.:
namespace xxx
{
public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static string[] userOverrides =
{
"constring1",
"constring2"
};
private static string userOverrideSuffix = "UserOverride";
public void SetUserOverride(string property, string value)
{
this[property] = value;
}
protected override void OnSettingsLoaded(object sender, System.Configuration.SettingsLoadedEventArgs e)
{
string userProperty = null;
foreach (string appProperty in userOverrides)
{
userProperty = appProperty + userOverrideSuffix;
if (((string)this[userProperty]).Length > 0)
{
this[appProperty] = this[userProperty];
}
}
}
protected override void OnSettingsSaving(object sender, System.ComponentModel.CancelEventArgs e)
{
string userProperty = null;
foreach (string appProperty in userOverrides)
{
userProperty = appProperty + userOverrideSuffix;
this[userProperty] = this[appProperty];
}
}
}
}
|
|
|
|

|
Hey. Great! I didn't realize that the normally VB only aplication framework would actually work in C#. Is it actually saving the changed settings on exit, or do you have to set some other properties and handlers in main()?
|
|
|
|

|
You should explicitly call Save() method
|
|
|
|

|
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?
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
This article describes a solution for persisting changes to the ReadOnly My.Settings.ConnectionString with nearly trivial code.
| Type | Article |
| Licence | CPOL |
| First Posted | 12 Jul 2007 |
| Views | 128,500 |
| Downloads | 727 |
| Bookmarked | 62 times |
|
|