Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:48:37+00:00 2026-05-15T13:48:37+00:00

I am trying to change the connectionString in my web.config using MSBUILD on a

  • 0

I am trying to change the connectionString in my web.config using MSBUILD on a Teamcity server. Previously I have use the attribute in a target that called this:

<PropertyGroup>
<UpdateWebConfigCode>
 <![CDATA[
  public static void ScriptMain()
  {
   XmlDocument wcXml = new XmlDocument();
   wcXml.Load(@"TCM.MVC.UI\Web.config");

   XmlElement root = wcXml.DocumentElement;
   XmlNodeList connList = root.SelectNodes("//connectionStrings/add");
   XmlElement elem;

   foreach (XmlNode node in connList)
   {
    elem = (XmlElement)node;

    switch (elem.GetAttribute("name"))
    {
     case "TCMBaseConnectionString":
      elem.SetAttribute("connectionString", "Data Source=server-name;Initial Catalog=TCMCentral;User ID=user;Password=something");
      break;

    }
   }

   wcXml.Save(@"TCM.MVC.UI\Web.config");          

  }
 ]]>
</UpdateWebConfigCode>

I would then call it in the target:

<Target Name="UpdateWebConfig">         
   <Script Language="C#" Code="$(UpdateWebConfigCode)" Imports="System.Xml" /> 
</Target>

But this keeps throwing an error. I realise this is probably a bit out of date but can’t find anything to replace it …. any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:48:38+00:00Added an answer on May 15, 2026 at 1:48 pm

    I do this with a custom task, which does much the same as your code, but the MSBuidl becomes:

    <UpdateConnectionString ConfigFile ="path\to\web.config"
             ConnectionStringName="MyConnectionStringName"
             ConnectionString="connection-string-here"/>
    

    The code for such a task is

    public class UpdateConnectionString : Task
        {
            [Required]
            public string ConfigFile { get; set; }
            [Required]
            public string ConnectionStringName { get; set; }
            [Required]
            public string ConnectionString { get; set; }
    
            public override bool Execute()
            {
                try
                {
                    var fi = new FileInfo(ConfigFile);
                    if(!fi.Exists)
                    {
                        Log.LogError("File {0} does not exist");
                        return false;
                    }
                    fi.IsReadOnly = false;
                    XDocument doc = XDocument.Load(ConfigFile);
                    var confignode = doc.Descendants("configuration").Single();
                    var connectionStrings = confignode.Descendants("connectionStrings").SingleOrDefault();
                    if(connectionStrings == null)
                    {
                        connectionStrings = new XElement("connectionStrings");
                        confignode.Add(connectionStrings);
                    }
                    var connectionElement = connectionStrings.Descendants("add").SingleOrDefault(
                        e => e.Attribute("name") != null &&
                             string.Compare(e.Attribute("name").Value, ConnectionStringName,
                                            StringComparison.OrdinalIgnoreCase) == 0);
                    if (connectionElement == null)
                    {
                        connectionElement = new XElement("add", new XAttribute("name", ConnectionStringName));
                        connectionStrings.Add(connectionElement);
                    }
    
                    connectionElement.SetAttributeValue("connectionString", ConnectionString);
                    doc.Save(ConfigFile);
                }
                catch (Exception ex)
                {
                    Log.LogErrorFromException(ex, true);
                    return false;
                }   
                return true;
            }
        }
    

    Note that this code will also add a connection string if its not present, this is probably more complexity than you need right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´m trying to change a spring jsp example to use freemarker. I changed all
I am trying to change my many to many script field to use FilteredSelectMultiple
I'm trying to use the XDT in a 4.0 web application and I just
all I am trying to change the connection string at runtime, i am using
Im trying to connect to a mysql database on my web server, but I
I am trying to use the ADO.NET Entity Data Model in a way that
I have a question on generics. I am trying to convert my code using
I have created a very simple ASP.NET application with a web.config file, which contains
I'm trying to programmatically encrypt configuration sections of App.config and Web.config files. In the
I have an asp.net web application that I made in Visual Studio 2008. Everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.