Home All Groups Group Topic Archive Search About

Can't set ConnectionStringSettings.ConnectionString property

Author
14 Feb 2006 6:57 PM
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.

Author
14 Feb 2006 9:17 PM
Michael Nemtsev
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
Author
14 Feb 2006 10:33 PM
Brad Wood
Michael Nemtsev wrote:
> 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'.
Author
15 Feb 2006 5:12 AM
Michael Nemtsev
Hello Brad,

Read here http://samples.gotdotnet.com/quickstart/aspplus/default.aspx?url=/quickstart/aspplus/doc/configretrieve.aspx

>> Hello Brad,
>>
>> u forgot <appSettings>
>>
>> <connectionStrings>
>> <appSettings>
>> <add name="production" connectionString="encrypted"
>> providerName="System.Data.OracleClient" />
>> </appSettings>
>> </connectionStrings>
BW> That doesn't work - Unrecognized element 'appSettings'.

---
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
Author
15 Feb 2006 6:07 PM
Brad Wood
Michael Nemtsev wrote:
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).
Author
15 Feb 2006 7:15 PM
Michael Nemtsev
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


Show quote
> 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
Author
15 Feb 2006 8:28 PM
Brad Wood
Michael Nemtsev wrote:
> 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?
Author
15 Feb 2006 8:51 PM
Michael Nemtsev
Hello Brad,

>> 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
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 Nietzsche
Author
16 Feb 2006 12:14 AM
Brad Wood
Michael Nemtsev wrote:
> 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?
Author
16 Feb 2006 1:06 PM
Christopher Reed
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 A. Reed
"The oxen are slow, but the earth is patient."

Show quote
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:ejHSzhZMGHA.3728@tk2msftngp13.phx.gbl...
> 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.
Author
16 Feb 2006 1:51 PM
Brad Wood
Christopher Reed wrote:
> 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?
Author
17 Feb 2006 3:26 AM
Christopher Reed
Is the file itself read only?
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Show quote
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:eG40VAwMGHA.1132@TK2MSFTNGP10.phx.gbl...
> Christopher Reed wrote:
>> 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?
Author
17 Feb 2006 7:07 AM
Brad
"Christopher Reed" wrote:

> 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.
Author
17 Feb 2006 12:51 PM
Christopher Reed
Are you accessing this web.config from your local machine or remotely?
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

Show quote
"Brad" <B***@discussions.microsoft.com> wrote in message
news:0B691E6E-13D5-46CE-938A-2DF7C12002B8@microsoft.com...
> "Christopher Reed" wrote:
>
>> 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.
Author
17 Feb 2006 5:30 PM
Brad
"Christopher Reed" wrote:

> 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();
    }

  }
}

AddThis Social Bookmark Button