Different ConnectionString for publishing/deploying Web Application

Hey there,
I’ve got a quick tip for you guys! Just now I was getting annoyed with the fact that after each filesystem publish I had to go and change the connectionstring to target my server SQL server instance, isntead of my development database. So I googled a while and found the following URL. I haven’t had the time to get it all figured out and tell you all about it but I’ve got it working! ūüôā

Basically what you do is, edit your Web.Release.config file in Vs.net, to tell Vs.Net which nodes in your Web.config need to be replaced before publishing. This file can be found under Web.config. There’s not much here by default, but that’s the point.

So, to update my connectionstring to the proper one for my server I had to do the following.

This is my original connectionstring as it is in my Web.config file:

<pre><span style="color:#339966;">&lt;configuration&gt;
  &lt;connectionStrings&gt;
    &lt;add name="EFDbContext" connectionString="Data Source=TOWER\TOWERSQL;
    Initial Catalog=SportStore;Persist Security Info=True;User ID=sa;Password=somepass;Pooling=False" providerName="System.Data.SqlClient"/&gt;
  &lt;/connectionStrings&gt;
&lt;/configuration&gt;</span></pre>

In my Web.Release.config, which has roughly the same structure as the regular config file, I added the following line:

<pre><span style="color: #339966;">&lt;configuration&gt;
&lt;connectionStrings&gt;
    &lt;add name="EFDbContext" connectionString="Data Source=BEGIJNHOF2\SQLBEGIJNHOF2;
    Initial Catalog=SportStore;Persist Security Info=True;User ID=sporter;Password=somepass;Pooling=False" providerName="System.Data.SqlClient"
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/&gt;
  &lt;/connectionStrings&gt;
&lt;/configuration&gt;</span></pre>

You can see the only difference is the xdt:Transform=”SetAttributes”¬†xdt:Locator=”Match(name)” part. This tells the builder that it has to find a node in the Web.config file that has the “EFDbContext” name, and has to replace all the attributes in that node by the ones I specify here, being the new connectionstring.

The only thing you need to do now, before you publish you set your build mode to “release” instead of “debug”, and Vs.Net will do the rest for you!

Nothing special, nothing new, but a handy trick! ūüôā

If there is something unclear, or it’s not working don’t hesitate to contact me or leave a comment! PS: I know the layout sucks big time, but this was a quick post and I’ll get back to it! I promise ūüôā

,Christophe

Advertisements

The credentials you provided for the SQL Server Agent service are invalid. To continue, provide a valid account and password for the SQL Server Agent service.

As a beginning .Net developer you probably had to install and/or reinstall SQL Server plenty of times, and so do I. Most of the time the SQL Server install isn’t a big hassle, but sometimes it tends to be a pain in the ass. One of the problems I encounter from time to time, while installing SQL Server whithout much attention, is the following error.

Image

This screen is somewhat confusing as it asks for a list of userlogins, which we obviously don’t know, we’re new here hello ūüôā

The “trick” here is to drop down the “Account Name” fields and select NT AUTHORITY\NETWORK SERVICE, without specifying a password. This makes the services of SQL Server boot using that system account, and according security restrictions.¬† This way you can continue your installation, and specify theSA credentials later on.

 

Image

 

Not much of a problem, but I’m guessing that if I ran into this for the second time now, there should be other people who run into this error! ūüôā

Peace,