ConfigurationSection properties cannot be edited when locked

Posted on 2006-04-11
Last Modified: 2008-01-09
I'm trying to add URL Mappings whenever a new page is added to the database, this is the code I have, but it keeps erroring out:

                  System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
                  UrlMappingsSection ums = (UrlMappingsSection)configuration.GetSection("system.web/urlMappings");
                  UrlMappingCollection umc = ums.UrlMappings;
                  UrlMapping um = new UrlMapping("~/" + sUrl, pageURL + "?pageID=" + pageID.ToString());            

Network Service and ASPNET both have read/write access to the file.

The error is:
Exception Details: System.InvalidOperationException: ConfigurationSection properties cannot be edited when locked.

on the configuration.Save() line
Question by:whityum
    LVL 17

    Expert Comment

    you can't modify the web.config file this way...
    you need to edit the file and save overwiting it
    LVL 7

    Author Comment

    This code is from the microsoft site:
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim myconf As Configuration = WebConfigurationManager.OpenWebConfiguration("/ASPNET20CC")
       Dim myUrls As UrlMappingsSection
       Dim myGroup As System.Web.Configuration.SystemWebSectionGroup = myconf.GetSectionGroup("system.web")
       myUrls = myGroup.UrlMappings
       myUrls.UrlMappings.Add(New UrlMapping(txtUrl.Text, txtZiel.Text))
       If Not myUrls.LockItem Then
       End If
    End Sub

    I converted it to C#, and even attempted to unlock the UrlMappingsSection and it still says ConfigurationSection properties cannot be edited when locked

                Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
                System.Web.Configuration.SystemWebSectionGroup swsg = (SystemWebSectionGroup)myconf.GetSectionGroup("system.web");
                UrlMappingsSection ums = swsg.UrlMappings;
                ums.LockItem = false;
                UrlMapping um = new UrlMapping("~/" + sUrl, pageURL + "?pageID=" + pageID.ToString());            
                ums.SectionInformation.ForceSave = true;
                return true;

    Has anyone added URL Mappings in code successfully?
    LVL 7

    Author Comment

    and lem2802, why do you say that?

    there is a whole management API to edit the configuration
    LVL 7

    Author Comment

    I changed Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); to

    Configuration myconf = WebConfigurationManager.OpenWebConfiguration("/projectName");

    and it works.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now