OK Here goes.
I have developped a VB.NET 2005 application which includes the fancy new "drag 'n drop" DataSet tool. When you first create a new dataset VB writes the connection string into the app.config file (visible through myproject.settings) as an "application scope" value. Application scope values are read only at runtime which is all well and good while I'm developping and my development server is available but completely useless when it comes to releasing the application to clients.
How can you change these connection strings at run time??
I've been searching now for ages to a proper answer to this question and a lot of time the answer is "you shouldn't do that....microsoft intended these to be fixed etc,etc" which is avoiding the issue. These values need to be changed!!!
One client has a need to set these values on a "per user" basis as the databases are replicted out to branch offices and the connectionstring needs to point at the replicated database.
The answer ould seem to be to change the values in the <programname>.exe.config file that is created in the user's directory and apply these in the settings.SettingsLoaded function but how exactly can this be done?
Simply assigning these values to a User Scope won't work as VB will continue to look at the application scope value for any updates effected on the dataset
I have seen Q_20954883 but it doesn't appear to address changing the connectionstring values (I accept that the app will need to be restarted the first time these are changed) as they don't appear to follow the normal XML structure rules.
Here's part of the app.config file...(values have been changed to protect the innocent)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration
.UserSetti
ngsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=123456789" >
<section name="System2.My.MySetting
s" type="System.Configuration
.ClientSet
tingsSecti
on, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=123456789" allowExeDefinition="Machin
eToLocalUs
er" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="Systemx.My.MySetting
s.Connecti
onString"
connectionString="Data Source=SQLSERVER;Initial Catalog=SystemXDB;Persist Security Info=True;User ID=xy;Password=abc"
providerName="System.Data.
SqlClient"
/>
<add name="Systemx.My.MySetting
s.Connecti
onString_O
LE"
connectionString="Provider
=SQLOLEDB;
Data Source=SQLSERVER;Initial Catalog=SystemXDB;Persist Security Info=True;User ID=xy;Password=abc"
providerName="System.Data.
OleDb" />
</connectionStrings>
<userSettings>
<Systemx.My.MySettings>
<setting name="Database" serializeAs="String">
<value>SystemxDB</value>
</setting>
<setting name="RunBefore" serializeAs="String">
<value>False</value>
</setting>
</System2.My.MySettings>
</userSettings>
</configuration>
I'm assigning maximum points to this as there are a lot of other developers asking the same question on lots of other such sites but please do not say "you shouldn't be doing this!!!" - unless of course there is another way of getting VB to connect the dataset to a runtime connectionstring
Many thanks
Ian
Start Free Trial