Can't set ConnectionStringSettings.ConnectionString property

  • Thread starter Thread starter Brad Wood
  • Start date Start date
B

Brad Wood

The ConnectionString property of the ConnectionStringSettings object is
get/set, but when I try to set it to some other value I get an error,
"The configuration is read only".

I get the object by calling
WebConfigurationManager.ConnectionStrings["production"]

From this Web.Config snippet:
<connectionStrings>
<add name="production" connectionString="encrypted"
providerName="System.Data.OracleClient" />
</connectionStrings>

I don't get it.
 
Hello Brad,

u forgot <appSettings>

<connectionStrings>
<appSettings>
<add name="production" connectionString="encrypted"
providerName="System.Data.OracleClient" />
</appSettings>
</connectionStrings>

BW> The ConnectionString property of the ConnectionStringSettings object
BW> is get/set, but when I try to set it to some other value I get an
BW> error, "The configuration is read only".
BW>
BW> I get the object by calling
BW> WebConfigurationManager.ConnectionStrings["production"]
BW>
BW> From this Web.Config snippet:
BW> <connectionStrings>
BW> <add name="production" connectionString="encrypted"
BW> providerName="System.Data.OracleClient" />
BW> </connectionStrings>
BW> I don't get it.
BW>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael said:
Hello Brad,

u forgot <appSettings>

<connectionStrings>
<appSettings>
<add name="production" connectionString="encrypted"
providerName="System.Data.OracleClient" />
</appSettings>
</connectionStrings>

That doesn't work - Unrecognized element 'appSettings'.
 
Michael said:

If I'm not mistaken, that page shows how to access config values the 1.0
way (ConfigurationSettings.AppSettings). The object to access the same
AppSettings collection in 2.0 is WebConfigurationManager.AppSettings
(there's a different object for winform apps).

My issue does not concern the AppSettings collection; it concerns the
ConnectionStrings collection that is new with 2.0. In 2.0 you can
declare a separate section like this in your config:

<connectionStrings>
<add name="test" connectionString="" providerName="System.Data.OleDb" />
<add name="production" connectionString=""
providerName="System.Data.OracleClient" />
</connectionStrings>

To access the test connection string in code you would call
WebConfigurationManager.ConnectionStrings["test"] to get a
ConnectionStringSettings object

All this works fine; I'm having trouble setting the connection string
property of the ConnectionStringSettings object after reading it in from
the config (I have it encrypted in the config, I need to decrypt it
after I read it in).
 
Ahh, I missed that u changes this values.
Well, I reckon it's impossible in this way.
Deal with this file with XML classes, open - find the node and change
values/attributes

The ConnectionString property of the ConnectionStringSettings object is
get/set, but when I try to set it to some other value I get an error,
"The configuration is read only".

I get the object by calling
WebConfigurationManager.ConnectionStrings["production"]

From this Web.Config snippet:
<connectionStrings>
<add name="production" connectionString="encrypted"
providerName="System.Data.OracleClient" />
</connectionStrings>

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael said:
Ahh, I missed that u changes this values.
Well, I reckon it's impossible in this way.
Deal with this file with XML classes, open - find the node and change
values/attributes

That would be way more trouble than it's worth - it would be easier to
simply put my connection string (and the provider invariant necessary to
create 2.0 generic DBFactories) back in my AppSettings collection.

My question remains, however - why the heck can't I call a property setter?
 
Hello Brad,
BW> That would be way more trouble than it's worth - it would be easier
BW> to simply put my connection string (and the provider invariant
BW> necessary to create 2.0 generic DBFactories) back in my AppSettings
BW> collection.
BW>
BW> My question remains, however - why the heck can't I call a property
BW> setter?

Because it's readonly property ;)

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
Michael said:
Hello Brad,
Because it's readonly property ;)

I don't get it; the object has a getter and a setter. If it were a read
only property, it would only have a getter. Right?
 
WebConfigurationSettings.ConnectionStrings["production"] gets the
ConnectionStringsSettings for the connection string named "production".
This is read only. However,
WebConfigurationSettings.ConnectionStrings["production"].ConnectionString
has get/set functionality and can be used to change the connection string
itself.
 
Christopher said:
WebConfigurationSettings.ConnectionStrings["production"] gets the
ConnectionStringsSettings for the connection string named "production".
This is read only. However,
WebConfigurationSettings.ConnectionStrings["production"].ConnectionString
has get/set functionality and can be used to change the connection string
itself.

Exactly. So why on earth am I getting "The configuration is read only"
when I try to change the connection string?
 
Is the file itself read only?
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Brad Wood said:
Christopher said:
WebConfigurationSettings.ConnectionStrings["production"] gets the
ConnectionStringsSettings for the connection string named "production".
This is read only. However,
WebConfigurationSettings.ConnectionStrings["production"].ConnectionString
has get/set functionality and can be used to change the connection string
itself.

Exactly. So why on earth am I getting "The configuration is read only"
when I try to change the connection string?
 
Christopher Reed said:
Is the file itself read only?

As a matter of fact it's not, but I don't want to update my config file; I
just want to convert what I read in from the config file (encrypted) to
decrypted. Nothing in the docs indicates that the ConnectionStringSettings
object does anything other than contain the properties that exist in my
config, and the ConnectionString property is get/set.
 
Christopher Reed said:
Are you accessing this web.config from your local machine or remotely?

Locally. The same error occurs when running a simple console app sample:

// add this element to an App.Config
<connectionStrings>
<add name="test" connectionString="string in config" providerName="" />
</connectionStrings>

// console app
using System;
using System.Configuration; // add reference

namespace ConsoleApplication1
{

class Program
{

static void Main( string[] args )
{
ConnectionStringSettings css =
ConfigurationManager.ConnectionStrings["test"];
Console.WriteLine( css.ConnectionString );
css.ConnectionString = "modified"; // run time error here
Console.ReadLine();
}

}
}
 
Back
Top