Managing ConnectionStrings in app.config


C

CaptainCaveman

Hi,

I'm trying to manage the Connection String from my app.config file.

I would like to read it in, amend it so that it contains a specific
datasource, then write it back.

My ConfigFile is named project.config, and the code I am trying to use
follows.

FILE ************************

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Project.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=MyPC\SQLEXPRESS;Initial
Catalog=Database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

CODE ***************************

ConnectionStringSettingsCollection cs =
ConfigurationManager.ConnectionStrings;

for (int nItem = 0; nItem < cs.Count; nItem++)
{
String str =
cs["Project.Properties.Settings.DatabaseConnectionString"].Name;
Console.WriteLine(str);
}

I keep getting an object not set to an instance of an object error. I
thought that the settings were loaded at runtime.

Could anyone help please in telling me how I have initialised this wrongly,
and how I should be reading the ConnectionStrings information.

TIA

CC
 
Ad

Advertisements

C

CaptainCaveman

I resolved being able to read teh string by making sur ehte config file was
in the same folder as my exe (schoolby error - I know), but how do you save
the ConnectionString if you have changed it?

TIA
 
C

Cor Ligthert[MVP]

With the save, I saw yesterday that it is not saved in the config in debug,
however it is done in the real app.exe.config

Cor

CaptainCaveman said:
I resolved being able to read teh string by making sur ehte config file was
in the same folder as my exe (schoolby error - I know), but how do you save
the ConnectionString if you have changed it?

TIA

CaptainCaveman said:
Hi,

I'm trying to manage the Connection String from my app.config file.

I would like to read it in, amend it so that it contains a specific
datasource, then write it back.

My ConfigFile is named project.config, and the code I am trying to use
follows.

FILE ************************

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Project.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=MyPC\SQLEXPRESS;Initial
Catalog=Database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

CODE ***************************

ConnectionStringSettingsCollection cs =
ConfigurationManager.ConnectionStrings;

for (int nItem = 0; nItem < cs.Count; nItem++)
{
String str =
cs["Project.Properties.Settings.DatabaseConnectionString"].Name;
Console.WriteLine(str);
}

I keep getting an object not set to an instance of an object error. I
thought that the settings were loaded at runtime.

Could anyone help please in telling me how I have initialised this
wrongly, and how I should be reading the ConnectionStrings information.

TIA

CC
 
Ad

Advertisements

M

miher

Hi,

This is one way to do it : (You need to reference System.Configuration)
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// select a connection string somehow
if (config.ConnectionStrings.ConnectionStrings.Count > 0)
{
// change some property
config.ConnectionStrings.ConnectionStrings[0].Name =
"NewName";
config.Save();
}

Hope You find this useful.
-Zsolt

CaptainCaveman said:
I resolved being able to read teh string by making sur ehte config file
was in the same folder as my exe (schoolby error - I know), but how do you
save the ConnectionString if you have changed it?

TIA

CaptainCaveman said:
Hi,

I'm trying to manage the Connection String from my app.config file.

I would like to read it in, amend it so that it contains a specific
datasource, then write it back.

My ConfigFile is named project.config, and the code I am trying to use
follows.

FILE ************************

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Project.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=MyPC\SQLEXPRESS;Initial
Catalog=Database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

CODE ***************************

ConnectionStringSettingsCollection cs =
ConfigurationManager.ConnectionStrings;

for (int nItem = 0; nItem < cs.Count; nItem++)
{
String str =
cs["Project.Properties.Settings.DatabaseConnectionString"].Name;
Console.WriteLine(str);
}

I keep getting an object not set to an instance of an object error. I
thought that the settings were loaded at runtime.

Could anyone help please in telling me how I have initialised this
wrongly, and how I should be reading the ConnectionStrings information.

TIA

CC
 

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