3

I have a few connection strings in my web.config that I want to remove using web.config transformation when I deploy my web app. I had found the MSDN page talking about web.config transformation but it only list a "Remove" which removes the first entry or the "RemoveAll" that deletes them all. Is there a way to only remove specific connection strings?

  <connectionStrings>
<add name="DB"
     connectionString="Data Source=; Initial Catalog=; User ID=; Password=;"
     providerName="System.Data.SqlClient" />
<add name="ErrorLog"
     connectionString="Data Source=; Initial Catalog=; User ID=; Password=;"
     providerName="System.Data.SqlClient" />
<add name="SiteFiles"
     connectionString="" />
<add name="FanFiles"
     connectionString="" />

  • Can you post some of your web.config (just use ... for the connection strings) so I can get an idea of how you have it set up? I think I can help but need a little more info. – rsbarro May 25 '13 at 00:11
  • @rsbarro I have added a few of my strings. Some are DB connections others are file store connects on Azure. These are only a few, I have about 20 strings. 5 or 6 of them used only for dev. –  May 25 '13 at 00:48

1 Answers1

5

To remove specific connection strings by name, you can use the following format:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

  <connectionStrings>
    <add name="ErrorLog" xdt:Transform="Remove" xdt:Locator="Match(name)" />
    <add name="SiteFiles" xdt:Transform="Remove" xdt:Locator="Match(name)" />
  </connectionStrings>

</configuration>

The transform xml above will remove the ErrorLog and SiteFiles connection strings. It uses the xdt:Locator attribute to match on the name attribute.

rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • do you know a way I can check to see if the transformation worked? I put the new code in and did a build soltution but when I do a windows find on the DIR that my solution is in I am not seeing any new web.config files. –  May 25 '13 at 17:22
  • Check this post: http://kfigy.blogspot.com/2010/03/making-visual-studio-2010-webconfig.html . They won't transform by default. I use msbuild and build scripts to apply transforms most of the time. The link I posted outlines a way to apply the transforms with an AfterBuild task in Visual Studio. Either way, that might be a separate question... =] – rsbarro May 25 '13 at 17:34
  • Thanks, I am almost done with a new page for my site so I will take a look at the web.config that gets pushed to Azure today when I publish my new update. –  May 25 '13 at 18:51
  • Unfortunaly that is not working. I get the error "Connection String argument cannot be null or empty". –  May 27 '13 at 00:35
  • Can you provide more detail about how it didn't work? Was any transform applied at all (do you see any changes in the deployed web.config versus the web.config in your project)? Do you have the transforms defined in a separate file? Have you gotten any other transforms to work properly? – rsbarro May 28 '13 at 14:24
  • When I do a publish I get the error "Connection String argument cannot be null or empty". If I remove the transformation then the publish works fine. –  May 28 '13 at 17:22
  • Where do you have the transformation defined, in your web.config? If so, that's not where they are supposed to go. They go in separate files that match the configuration you are building for. You should be able to expand the web.config file (click the little plus next to it in Visual Studio Solution Explorer) to see the transform files. See here for more info: http://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx – rsbarro May 28 '13 at 17:41
  • It is in the web.release.config file along with other transformations that work just fine. –  May 28 '13 at 17:47
  • OK, so maybe try adding a `connectionString=""` attribute to the transform xml, or possibly `connectionString="none"`? The transform xml I supplied worked fine for me in my test project, but I use msbuild to apply the transforms, not publish. – rsbarro May 28 '13 at 19:25
  • I did try adding the connectionString attribute but that did not work. I tried it as "", as "none", and even putting the full connectionString that is in the top level web.config, all produced the same error. –  May 28 '13 at 19:57
  • These links might help. I think the issue may be that **after** the transform is complete there is a connectionString attribute that is null or empty...? See http://stackoverflow.com/questions/8920451/how-to-remove-a-connectionstring-using-config-transformations or see the comment here http://stackoverflow.com/questions/11580680/vs2010-web-deploy-the-connection-string-argument-cannot-be-null. – rsbarro May 29 '13 at 01:58