MSBuild Invalid Character in Connection String with dbproj

Date Published: 03 February 2011

MSBuild Invalid Character in Connection String with dbproj

If you get this error:

error MSB5016: The name “Intitial Catalog” contains an invalid character “ “.

or something similar when trying to specify a connection string within an MSBuild task, like this one:

&lt;MSBuild Projects=<span style="color: #006080">&quot;srcDatabasesNorthwind.dbproj&quot;</span> 
    Targets=<span style="color: #006080">&quot;Build;Deploy&quot;</span> 
        Properties=<span style="color: #006080">&quot;TargetDatabase=Northwind;TargetConnectionString=&quot;Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User Id=foo;password=bar;Enlist=False;&quot;&quot;</span> /&gt;
        &#160;
        &#160;

The issue is with the semicolons in the querystring. If you replace them with %3B then the error goes away:

&lt;MSBuild Projects=<span style="color: #006080">&quot;srcDatabasesNorthwind.dbproj&quot;</span> 
Targets=<span style="color: #006080">&quot;Build;Deploy&quot;</span> 
Properties=<span style="color: #006080">&quot;TargetDatabase=Northwind;TargetConnectionString=&quot;Data&#x20;Source=localhost%3BInitial&#x20;Catalog=Northwind%3BPersist&#x20;Security&#x20;Info=True%3BUser&#x20;Id=foo%3Bpassword=bar%3BEnlist=False%3B&quot;&quot;</span> /&gt;
&#160;
&#160;

Hope this helps! You can also escape the spaces with but I don’t believe that’s required.

Steve Smith

About Ardalis

Software Architect

Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.