Modify app.config in VB.NET

B

Bill Yanaire

I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config?

The string is in the following:

<connectionStrings>

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLMachine to MySQLMachine_1



Thank you.
 
M

Mr. Arnold

Bill Yanaire said:
I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config?

The string is in the following:

<connectionStrings>

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLMachine to MySQLMachine_1

What do you mean? Do you want to change it during program execution with a
new value being picked-up?
 
K

kimiraikkonen

I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config?

The string is in the following:

<connectionStrings>

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>

I would like to change the DataSource from MySQLMachine to MySQLMachine_1

Thank you.

App.config XML file can be opened and edited by numerous apps such as
Notepad and VS versions if you want to change the values manually.
 
J

JDS

App.config XML file can be opened and edited by numerous apps such as
Notepad and VS versions if you want to change the values manually.

I have had a similar problem trying to change the value from code in
VS2005; I get an error saying that it is read only (or something
similar; it is a while since I looked at it). If there is a solution
I'd greatly appreciate it if anyone knows how.
 
B

Bill Yanaire

Mr. Arnold said:
What do you mean? Do you want to change it during program execution with a
new value being picked-up?

Yes, I would like to change the DataSource when the program starts
execution. Depending on what machine the program is run, I would like to
check the name of the computer then change accordingly.
 
M

Mr. Arnold

Bill Yanaire said:
Yes, I would like to change the DataSource when the program starts
execution. Depending on what machine the program is run, I would like to
check the name of the computer then change accordingly.

Yes, you can do that. You can get the machine name.

Public Function GetMyDnsName() As String
Return System.Net.Dns.GetHostName()
End Function

Now, if if want to manipulate app.config, then you'll be working with
pgmname.app.config. That's what the program uses and not app.config doing
program execution.

You can download the Application Blocks for 2.0 .Net Framework, which will
have a section of source code and a project that you can look at that works
on the app.config, with reading, writing and changing app.config data during
program execution. There should be a download for 3.0, 3.5 or you can apply
the source code from 2.0, look for it using Google.
 
P

Peter Forman

Thanks for the link, but that doesn't allow me to modify a connection
string. The code looks like it reads only.


something along these lines....

Public Shared Sub ChangeConnectionString(ByVal strConn As String)
Dim _config As System.Configuration.Configuration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
'the full name of the connection string can be found in the
app.config file
' in the "name" attribute of the connection string

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn
'Save to file

_config.Save(System.Configuration.ConfigurationSaveMode.Modified)
'force changes to take effect so that we can start using
'this new connection string immediately

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub


....worked for me.
 
B

Bill Yanaire

Peter Forman said:
something along these lines....

Public Shared Sub ChangeConnectionString(ByVal strConn As String)
Dim _config As System.Configuration.Configuration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
'the full name of the connection string can be found in the
app.config file
' in the "name" attribute of the connection string

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn
'Save to file

_config.Save(System.Configuration.ConfigurationSaveMode.Modified)
'force changes to take effect so that we can start using
'this new connection string immediately

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub


...worked for me.

I just tried to copy your code into a test application. Received an error
'System.Configuration is not defined'. I have an 'Imports
System.Configuration' at the top of the form.

Any idea why?

Thanks
 
H

Herfried K. Wagner [MVP]

Bill Yanaire said:
I just tried to copy your code into a test application. Received an error
'System.Configuration is not defined'. I have an 'Imports
System.Configuration' at the top of the form.

Make sure your project contains a reference to "System.Configuration.dll".

Note that the code above will only work if the user has write privileges for
the config file, which is by default not the case for normal users.
 
B

Bill Yanaire

Herfried K. Wagner said:
Make sure your project contains a reference to "System.Configuration.dll".

Note that the code above will only work if the user has write privileges
for the config file, which is by default not the case for normal users.

Thanks for the tip on the System.Configuration.dll. That corrected the
errors. Regarding the following:

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn


doesn't this change the MySettings or will it also change the
<connectionStrings>? I would like to change the

<connectionStrings>

<add name="ConnString" connectionString="Data Source=MySQLServer;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLServer to YourSQLServer.




Thanks
 
A

Andrew

Hi,

I've tried the code out with reference from msdn and I've got the same
problem as well.

When going thru the stepbystep debugger, I can see the values being added to
the ConfigurationManager, but somehow the values are not being save to the
app.config file.

eg.

using System.Configuration;

ConfigurationSection section =
(ConfigurationSection)config.GetSection("appSettings");

//Read the key in the app.config file
AppSettingsSection assection = config.AppSettings;
string str = assection.Settings["aaa"].Value.ToString();

// Add an Application Setting.
config.AppSettings.Settings.Add(asName, "test_value");

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Is there an error in my code ? Any feedback is appreciated.

cheers,
Andrew
 
L

Levent Dagistanli

I think you are running your code from VS. Just run the exe from outside and
it will work.
Thanks,
Levent Dagistanli
 
M

Mike Williams

I think you are running your code from VS. Just run the exe from outside
and
it will work.

I don't know whether anyone from Microsoft regularly reads the Visual Basic
groups but if they do then I am very surprised that they permit one of their
own MVPs to engage in such outrageous long term trolling activities in one
of their own public newsgroups, such as the activity that the person who
purports to be Bill McCarthy has engaged in on the
microsoft.public.vb.general.discussion group for many months. If this man
belongs to you:

https://mvp.support.microsoft.com/profile=B2D0BB02-3E35-4293-B4B9-25680609CCB8

.. . . then perhaps you might like to look at his activity in that group.
Here for example is one of his very latest offerings:
 
S

Saurabh Yadav

Thanks a lot for this..... Peter Forman

I've been lookin for this code from long time.... thanks for your valuable help....!!



Peter Forman wrote:

Re: Modify app.config in VB.NET
07-Mar-08


something along these lines...

Public Shared Sub ChangeConnectionString(ByVal strConn As String
Dim _config As System.Configuration.Configuration
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None
'the full name of the connection string can be found in th
app.config fil
' in the "name" attribute of the connection strin

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionStrin
= strCon
'Save to fil

_config.Save(System.Configuration.ConfigurationSaveMode.Modified
'force changes to take effect so that we can start usin
'this new connection string immediatel

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name
My.Settings.Reload(
End Su

....worked for me.

Previous Posts In This Thread:

Modify app.config in VB.NET
I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config

The string is in the following

<connectionStrings

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/


I would like to change the DataSource from MySQLMachine to MySQLMachine_


Thank you.

Bill,Do you mean this?
Bill

Do you mean this

http://msdn2.microsoft.com/en-us/li...on.configurationmanager.connectionstrings.asp

Cor

Re: Modify app.config in VB.NET
What do you mean? Do you want to change it during program execution with
new value being picked-up?

Re: Modify app.config in VB.NET
App.config XML file can be opened and edited by numerous apps such a
Notepad and VS versions if you want to change the values manually.

Re: Modify app.config in VB.NET

nitia
=3Dsa1234


I have had a similar problem trying to change the value from code i
VS2005; I get an error saying that it is read only (or somethin
similar; it is a while since I looked at it). If there is a solutio
I'd greatly appreciate it if anyone knows how.

Re: Modify app.config in VB.NET
Yes, I would like to change the DataSource when the program start
execution. Depending on what machine the program is run, I would like t
check the name of the computer then change accordingly.

Re: Modify app.config in VB.NET
Thanks for the link, but that does not allow me to modify a connectio
string. The code looks like it reads only.

Re: Modify app.config in VB.NET

Yes, you can do that. You can get the machine name

Public Function GetMyDnsName() As Strin
Return System.Net.Dns.GetHostName(
End Functio

Now, if if want to manipulate app.config, then you'll be working with
pgmname.app.config. That's what the program uses and not app.config doing
program execution

You can download the Application Blocks for 2.0 .Net Framework, which will
have a section of source code and a project that you can look at that works
on the app.config, with reading, writing and changing app.config data during
program execution. There should be a download for 3.0, 3.5 or you can apply
the source code from 2.0, look for it using Google.

Re: Modify app.config in VB.NET

I just tried to copy your code into a test application. Received an error
'System.Configuration is not defined'. I have an 'Imports
System.Configuration' at the top of the form.

Any idea why?

Thanks

Re: Modify app.config in VB.NET

Make sure your project contains a reference to "System.Configuration.dll".

Note that the code above will only work if the user has write privileges for
the config file, which is by default not the case for normal users.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Re: Modify app.config in VB.NET

Thanks for the tip on the System.Configuration.dll. That corrected the
errors. Regarding the following:

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn


doesn't this change the MySettings or will it also change the
<connectionStrings>? I would like to change the

<connectionStrings>

<add name="ConnString" connectionString="Data Source=MySQLServer;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLServer to YourSQLServer.




Thanks

Re: Modify app.config in VB.NET


something along these lines....

Public Shared Sub ChangeConnectionString(ByVal strConn As String)
Dim _config As System.Configuration.Configuration =
System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
'the full name of the connection string can be found in the
app.config file
' in the "name" attribute of the connection string

_config.ConnectionStrings.ConnectionStrings("<##yourprojectname##>.My.MySettings.<###YourConnectionString###>").ConnectionString
= strConn
'Save to file

_config.Save(System.Configuration.ConfigurationSaveMode.Modified)
'force changes to take effect so that we can start using
'this new connection string immediately

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub


....worked for me.

Hi,I've tried the code out with reference from msdn and I've got the same
Hi,

I've tried the code out with reference from msdn and I've got the same
problem as well.

When going thru the stepbystep debugger, I can see the values being added to
the ConfigurationManager, but somehow the values are not being save to the
app.config file.

eg.

using System.Configuration;

ConfigurationSection section =
(ConfigurationSection)config.GetSection("appSettings");

//Read the key in the app.config file
AppSettingsSection assection = config.AppSettings;
string str = assection.Settings["aaa"].Value.ToString();

// Add an Application Setting.
config.AppSettings.Settings.Add(asName, "test_value");

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Is there an error in my code ? Any feedback is appreciated.

cheers,
Andrew

I think you are running your code from VS.
I think you are running your code from VS. Just run the exe from outside and
it will work.
Thanks,
Levent Dagistanli

:

Re: Modify app.config in VB.NET
message

I don't know whether anyone from Microsoft regularly reads the Visual Basic
groups but if they do then I am very surprised that they permit one of their
own MVPs to engage in such outrageous long term trolling activities in one
of their own public newsgroups, such as the activity that the person who
purports to be Bill McCarthy has engaged in on the
microsoft.public.vb.general.discussion group for many months. If this man
belongs to you:

https://mvp.support.microsoft.com/profile=B2D0BB02-3E35-4293-B4B9-25680609CCB8

.. . . then perhaps you might like to look at his activity in that group.
Here for example is one of his very latest offerings:



Submitted via EggHeadCafe - Software Developer Portal of Choice
EggHeadCafe Chat Chaos in Silverlight Released Today
http://www.eggheadcafe.com/tutorial...6-54f31bdede5d/eggheadcafe-chat-chaos-in.aspx
 
F

Fahad AlEnizi

Hi Mr.Peter Forman

I have tryed your code and i have modified them to this:

Public Shared Sub ChangeConnectionString()
Dim _config As System.Configuration.Configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
_config.ConnectionStrings.ConnectionStrings("POS_Inventory.My.MySettings.POS_DatabaseConnectionString").ConnectionString = "TEST"
_config.Save(System.Configuration.ConfigurationSaveMode.Modified)

System.Configuration.ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name)
My.Settings.Reload()
End Sub

but it is still not work for me I don't know why?
is there any problem in my code?

many thanks
Fahad AlEnizi
I would like to change a connection string in my Windows application (VS
2008). Is there a way to modify the connection string in app.config?

The string is in the following:

<connectionStrings>

<add name="StringOne" connectionString="Data Source=MySQLMachine;Initial
Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa1234"
providerName="System.Data.SqlClient"/>



I would like to change the DataSource from MySQLMachine to MySQLMachine_1



Thank you.
On Tuesday, March 04, 2008 11:53 PM Cor Ligthert[MVP] wrote:
Bill,

Do you mean this?

http://msdn2.microsoft.com/en-us/li...n.configurationmanager.connectionstrings.aspx

Cor
On Thursday, March 13, 2008 12:10 PM Andre wrote:
Hi,

I've tried the code out with reference from msdn and I've got the same
problem as well.

When going thru the stepbystep debugger, I can see the values being added to
the ConfigurationManager, but somehow the values are not being save to the
app.config file.

eg.

using System.Configuration;

ConfigurationSection section =
(ConfigurationSection)config.GetSection("appSettings");

//Read the key in the app.config file
AppSettingsSection assection = config.AppSettings;
string str = assection.Settings["aaa"].Value.ToString();

// Add an Application Setting.
config.AppSettings.Settings.Add(asName, "test_value");

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");

Is there an error in my code ? Any feedback is appreciated.

cheers,
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top