ConfigurationSection properties cannot be edited when locked

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());            
                  umc.Add(um);
                  configuration.Save(ConfigurationSaveMode.Full);

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
LVL 7
whityumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elvio Lujan.Net Senior DeveloperCommented:
you can't modify the web.config file this way...
you need to edit the file and save overwiting it
0
whityumAuthor Commented:
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
            myconf.Save()
   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.UrlMappings.Add(um);                  
            ums.SectionInformation.ForceSave = true;
            myconf.Save(ConfigurationSaveMode.Full);
            return true;

Has anyone added URL Mappings in code successfully?
0
whityumAuthor Commented:
and lem2802, why do you say that?

there is a whole management API to edit the configuration
http://asp.net/QuickStart/aspnet/doc/management/mgmtapi.aspx
0
whityumAuthor Commented:
I changed Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~/Web.config"); to

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

and it works.
0
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.