Inserting and Updating List Items with ASP.Net MVC and JQuery

May 16, 2013

In ASP.Net MVC you can display lists of data either as a grid with paging, just as in ASP.Net, or, as I prefer most often, using Infinite Scrolling.

Editing and item or adding an item to the list needs a different approach in Infinite Scrolling as opposed to the paging grid: after adding or updating, you do not want the user to have to scroll back to the original location of the record inside the grid.

Let’s have a look at how Infinite Scrolling can be implemented:

$.get("/More/Items/" + page /* + other options, such as sort order */ , 
  function(data) {    if (data != "") {
      $("#myList").append(data);
    }
  }
);

So we need a controller method returning a partial view filled with the data model of the list.

To support adding a newly created record, or displayed an updated record in the list, we split the list’s partial view into an enumeration part and an item part

List.ascx:

<table><thead><tr><th>.....</th></tr></thead><tbody>
<% 
  foreach(var item in Model.Items) {
    Html.RenderPartial("ListItem", item);
  }
%>
</tbody></table>

The ListItem.ascx then contains the record inside a <tr>:

<tr><td>.....</td>....</tr>

Easy.

Next. we want to support adding and editing records. Personally I prefer jQuery UI’s $.dialog() to open a form for editing the record, and upon closing the dialog, the data is stored using an Ajax or Web service request, and finally the list item is updated:

var tbl = $("#myTable tbody");

$.get('Ajax/GetInsertedRecord?id=' + data.Id
  function (data) {
    tbl.prepend(data);    // insert as first record in list
  }
);

where data is the record returned from the Ajax call to insert the data.

Analogously for updating the edited record, use

var tr = hl.parent().parent();  // retrieve the <tr> of the Edit button

$.get('Ajax/GetUpdatedRecord?id=' + data.Id,
  function (data) {
    tr.replaceWith(data);
  }
);

This in-place adding and updating just require 2 controller methods, and the record partial view extracted from the original list view! Both controller methods return the same partial view as the one referenced in the list partial view.


SMOscript and Change Tracking

May 7, 2013

SMOscript user Robert pointed out that SMOscript did not support the Change Tracking option of the ScriptingOptions parameter class.

Indeed, of the new Change Tracking features found in SQL Server 2008 and higher, SMO only supports scripting Change Tracking, but does not generate the commands to script Change Data Capture.

This seems to be a known omission of SMO, and it will not be implemented any time soon:

We took a look at this DCR (this is not defect but actually DCR to add suport of SMO to CDC) along with several others. Unfortunately based on current customers votes count we decided not to proceed with this DCR in the next release. However, we have taken note of this internally, and when we revisit this functionality in the future, we will try and get this implemented.

which is kind of strange considering you use SMO to re-create an existing database schema in a new schema, and maybe your T-SQL business logic relies on objects created by CDC.

Nonetheless, SMOscript 0.20 now also create the ENABLE CHANGE_TRACKING if used with the -ct switch.

As usual, the latest version of SMOscript is available for download here.


Change Tracking and Change Data Capture

May 7, 2013

SQL Server 2008 introduced 2 features that allow tracking changes to table data, namely Change Tracking and Change Data Capture.

Change Tracking is enabled on database level using the statement

ALTER DATABASE [mydb]
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)

and on table level using

ALTER TABLE [myTable]
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON)

To query the server which databases are actually tracking-enabled, use the following query

SELECT * FROM [sys].[change_tracking_databases]

The list of tracking-enabled tables can be retrieved by this statement

SELECT * FROM [sys].[change_tracking_tables]

Change Data Capture is enabled using the stored procedure sys.sp_cdc_enable_on:

EXEC sys.sp_cdc_enable_db

If your database editition does not support CDC, you will receive the error:

Msg 22988, Level 16, State 1, Procedure sp_cdc_enable_db, Line 12
This instance of SQL Server is the [***] Edition. Change data capture is only available in the Enterprise, Developer, and Enterprise Evaluation editions.

CDC adds the cdc schema and other database objects

When a database is enabled for change data capture, the cdc schema, cdc user, metadata tables, and other system objects are created for the database.

so the easiest method to detect whether CDC is enabled is to check for the [cdc] schema.

A table can be enabled for CDC using the SP sys.sp_cdc_enabled_table:

EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name   = N'MyTable',
@role_name     = N'MyRole',
@filegroup_name = N'MyDB_CT',
@supports_net_changes = 1

The parameter @role_name must be provided, but can be NULL. The following parameters are optional.

To find out which tables are CDC-enabled, use the SQL statement

SELECT * FROM [cdc].[change_tables]

On Converting Data

May 2, 2013

I had to analyze SQL Server Database Projects (available from SQL Server Data Tools for Visual Studio), and these projects offer a menu item “Create Snapshot” which creates a snapshot file with the extension .dacpac.

It turns out that a .dacpac is a zipped XML file (plus some other files) containing a structured representation of the database objects defined in the project. However Visual Studio does not provide a way to display them (double-clicking the file will only display binary data).

So I thought about how to best display the contents of a .dacpac? Two methods came to my mind.

First, inspired by my work on wpxslgui, create an XSLT style sheet which transforms the contents of the XML file to some legible text, for example CREATE TABLE and similar TSQL statements.

Intuitively I called this approach Symbolic Transformation.

Symbolic Transformation (e.g. XSLT)
Representation 1 => Representation 2

On the other hand, a Logical Transformation contains one module parsing the information contained in “Representation 1″ into some kind of model, and another module creating the “Representation 2″ of that model. The two module can be  implementations of the Interpreter pattern and the Builder or Factory patterns, respectively.

Logical Transformation (simple)
Interpreter Builder
Representation 1 => Model => Representation 2

If we take Representation 1 and Representation 2 as two separate interfaces to the same business model, and want to support two-way operations, we can extend the last table like this:

Logical Transformation (extended)
Interpr.
Builder
Converter Converter Interpr.
Builder
Repr. 1 => Model 1 => Business Model => Model 2 => Repr. 2
<= <= <= <=

Why do we need to have Model 1 and Model 2, as they seem to make the whole thing even more complex?

Let’s have a look at a simple CREATE TABLE statement and some of their representations:

  • a SQL parser (think: ANTLR) uses the representation given by the SQL parser
  • the SQL Server catalog views sys.tables, sys.columns, etc. are a different representation
  • a .dacpac archive is another representation

To keep our code simple, our Model X class structure should be as close to the representation as 1) possible 2) necessary (thinking about proxy classes generated by xsd.exe, ANTLR, and ORMs).

Thus, a common data model (named Business Model in the table) is required, as well as 2-way conversion between the Business Model and each of the other models.


Innovation Treadmill

April 30, 2013

The longer a software product exists and is being actively developed, the more changes are going to affect its development.

The kinds of changes are various, ranging from new technology, new development tools, new development paradigms and trends, to changing requirements and solutions, or complete shift of focus.

I’ll just summarize some of these changes that may apply to my projects (and probably yours as well):

Old New
Framework ASP.Net ASP.Net MVC
PostBack Ajax, In-page update
Client-side scripting Ajax Control Toolkit jQuery
Javascript TypeScript
Data Access SqlCommand (aspx, C#) Data Access Layer
XML Generation StringBuilder XSD, xsd, XmlSerializer
Parser DIY NIH Antlr
Focus (e.g. VSSlnVis) Graphical representation of dependencies Textual analysis of solutions and projects
(e.g. dbscript) Generate C# constants, Generate CREATE scripts database versioning, documentation and deployment
Patterns? Anti-Patterns, God Objects, Spaghetti Patterns, Lasagna, Ravioli

If your project finds itself in a sort of identity crisis, then don’t worry. Companies and organizations bigger than yours often recognize the need to re-write:

KDE did it, Mozilla did it, phpBB did it, and Google just forked Webkit into Blink to be used in Chrome.

So I came to the conclusion that to increase maintainability and extensibility of the programs and projects that I still update (some more frequently, others less), I need to rework their code.

As I prefer my principle of the least technological requirements, I am still not sure whether to stick with .Net 2 for my WinForms apps, or whether .Net 4 is already acceptable (i.e. does not require the user to separately install specific versions of .Net).

Please leave a comment with your ideas ;)


If you have a template, someone needs to process it, too

April 29, 2013

Just came along this post by Scott Hanselman, and I feel the need to share it, so that the template bots have the opportunity for commenting on their template ;)

(My guess is that the template engine does not support nested templates, but who knows?)

{
{I have|I've} been {surfing|browsing} online more than {three|3|2|4} hours 
today, yet I never found any interesting article like yours. {It's|It
is} pretty worth enough for me. {In my opinion|Personally|In my view}, 
if all {webmasters|site owners|website owners|web owners} and bloggers 
made good content as you did, the {internet|net|web} will be 
{much more|a lot more} useful than ever before.|
I {couldn't|could not} {resist|refrain from} commenting. 
{Very well|Perfectly|Well|Exceptionally well} written!|
{I will|I'll} {right away|immediately} {take hold of|grab|clutch|grasp|seize|snatch}
your {rss|rss feed} as I {can not|can't} {in finding|find|to find} your 
{email|e-mail} subscription {link|hyperlink} or {newsletter|e-newsletter} 
service. Do {you have|you've} any?
{Please|Kindly} {allow|permit|let} me {realize|recognize|understand|recognise|know} 
{so that|in order that} I {may just|may|could} subscribe.
Thanks.|
{It is|It's} {appropriate|perfect|the best} time to make some plans for 
the future and {it is|it's} time to be happy.
{I have|I've} read this post and if I could I {want to|wish to|desire to} suggest 
you {few|some} interesting things or {advice|suggestions|tips}. {Perhaps|Maybe} 
you {could|can} write next articles referring to this article. I {want to|wish to|desire to} 
read {more|even more} things about it!|
{It is|It's} {appropriate|perfect|the best} time to make {a few|some} plans for 
{the future|the longer term|the long run} and {it is|it's} time to be happy. 
{I have|I've} {read|learn} this {post|submit|publish|put up} and
if I {may just|may|could} I {want to|wish to|desire to} {suggest|recommend|counsel} 
you {few|some} {interesting|fascinating|attention-grabbing} {things|issues} or 
{advice|suggestions|tips}.
{Perhaps|Maybe} you {could|can} write {next|subsequent} articles {relating to|referring to|regarding} 
this article.
I {want to|wish to|desire to} {read|learn} {more|even more} {things|issues} 
{approximately|about} it!|{I have|I've} been {surfing|browsing} {online|on-line} 
{more than|greater than} {three|3} hours {these days|nowadays|today|lately|as of late}, 
{yet|but} I {never|by no means} {found|discovered} any {interesting|fascinating|attention-grabbing} 
article like yours. {It's|It is} {lovely|pretty|beautiful} {worth|value|price} 
{enough|sufficient} for me. {In my opinion|Personally|In my view},
if all {webmasters|site owners|website owners|web owners} and bloggers made 
{just right|good|excellent} {content|content material} as {you did|you probably did}, 
the {internet|net|web} {will be|shall be|might be|will probably be|can be|will likely be} 
{much more|a lot more} {useful|helpful} than ever before.| 
Ahaa, its {nice|pleasant|good|fastidious} {discussion|conversation|dialogue} 
{regarding|concerning|about|on the topic of} this {article|post|piece of writing|paragraph} 
{here|at this place} at this {blog|weblog|webpage|website|web site}, I have read all
that, so {now|at this time} me also commenting {here|at this place}.|
I am sure this {article|post|piece of writing|paragraph}
has touched all the internet {users|people|viewers|visitors}, its really really 
{nice|pleasant|good|fastidious} {article|post|piece of writing|paragraph} on
building up new {blog|weblog|webpage|website|web site}.
|
Wow, this {article|post|piece of writing|paragraph} is {nice|pleasant|good|fastidious}, my
{sister|younger sister} is analyzing {such|these|these kinds of} things, 
{so|thus|therefore} I am going to {tell|inform|let know|convey} her.|
{Saved as a favorite|bookmarked!!}, {I really like|I
like|I love} {your blog|your site|your web site|your website}!
|
Way cool! Some {very|extremely} valid points! I
appreciate you {writing this|penning this} {article|post|write-up} 
{and the|and also the|plus the} rest of the {site is|website is} 
{also very|extremely|very|also really|really} good.
|
Hi, {I do believe|I do think} {this is an excellent|this is a great} 
{blog|website|web site|site}. I stumbledupon it ;)  {I will|I am going
to|I'm going to|I may} {come back|return|revisit} {once again|yet again} 
{since I|since i have} {bookmarked|book marked|book-marked|saved as a favorite} 
it. Money and freedom {is the best|is the greatest} way to change, may you be 
rich and continue to {help|guide} {other people|others}.|
Woah! I'm really {loving|enjoying|digging} the template/theme of this 
{site|website|blog}.
It's simple, yet effective. A lot of times it's {very hard|very difficult|challenging|tough|difficult|hard} 
to get that "perfect balance" between {superb usability|user friendliness|usability} 
and {visual appearance|visual appeal|appearance}.
I must say {that you've|you have|you've} done a {awesome|amazing|very good|superb|fantastic|excellent|great} 
job with this. {In addition|Additionally|Also}, the blog loads {very|extremely|super} 
{fast|quick} for me on {Safari|Internet explorer|Chrome|Opera|Firefox}.
{Superb|Exceptional|Outstanding|Excellent} Blog!
|
These are {really|actually|in fact|truly|genuinely} {great|enormous|impressive|wonderful|fantastic} ideas
in {regarding|concerning|about|on the topic of} blogging.
You have touched some {nice|pleasant|good|fastidious} {points|factors|things}
here. Any way keep up wrinting.|
{I love|I really like|I enjoy|I like|Everyone loves} what you guys
{are|are usually|tend to be} up too. {This sort
of|This type of|Such|This kind of} clever work and {exposure|coverage|reporting}!

Keep up the {superb|terrific|very good|great|good|awesome|fantastic|excellent|amazing|wonderful} 
works guys I've {incorporated||added|included} you guys to {|my|our||my personal|my own} 
blogroll.|
{Howdy|Hi there|Hey there|Hi|Hello|Hey}! Someone in my {Myspace|Facebook} group shared 
this {site|website} with us so I came to {give it a look|look it over|take a look|check it out}. 
I'm definitely {enjoying|loving} the information.
I'm {book-marking|bookmarking} and will be tweeting this to my followers! 
{Terrific|Wonderful|Great|Fantastic|Outstanding|Exceptional|Superb|Excellent} blog and 
{wonderful|terrific|brilliant|amazing|great|excellent|fantastic|outstanding|superb} 
{style and design|design and style|design}.|
{I love|I really like|I enjoy|I like|Everyone loves} what you guys {are|are usually|tend to be} 
up too. {This sort of|This type of|Such|This kind of} clever work and {exposure|coverage|reporting}! 
Keep up the {superb|terrific|very good|great|good|awesome|fantastic|excellent|amazing|wonderful} 
works guys I've {incorporated|added|included} you guys to
{|my|our|my personal|my own} blogroll.|
{Howdy|Hi there|Hey there|Hi|Hello|Hey} would you mind {stating|sharing} which blog 
platform you're {working with|using}? I'm {looking|planning|going} to start my own blog 
{in the near future|soon} but I'm having a {tough|difficult|hard} time 
{making a decision|selecting|choosing|deciding} between BlogEngine/Wordpress/B2evolution and Drupal. 
The reason I ask is because your {design and style|design|layout} seems different then 
most blogs and I'm looking for something {completely unique|unique}. P.S 
{My apologies|Apologies|Sorry} for {getting|being} off-topic but
I had to ask!|
{Howdy|Hi there|Hi|Hey there|Hello|Hey} would you mind letting
me know which {webhost|hosting company|web host} you're {utilizing|working with|using}? 
I've loaded your blog in 3 {completely different|different} {internet browsers|web browsers|browsers} 
and I must say this blog loads a lot {quicker|faster} then most.

Can you {suggest|recommend} a good {internet
hosting|web hosting|hosting} provider at a {honest|reasonable|fair}
price? {Thanks a lot|Kudos|Cheers|Thank you|Many thanks|Thanks}, I
appreciate it!|
{I love|I really like|I like|Everyone loves} it {when people|when individuals|when folks|whenever people} 
{come together|get together} and share {opinions|thoughts|views|ideas}. 
Great {blog|website|site}, {keep it up|continue the good work|stick with it}!|
Thank you for the {auspicious|good} writeup. It in fact was a amusement account it.
Look advanced to {far|more} added agreeable from you! {By the way|However}, how 
{can|could} we communicate?
|
{Howdy|Hi there|Hey there|Hello|Hey} just wanted to give you a quick heads up.

The {text|words} in your {content|post|article} seem to be running off
the screen in {Ie|Internet explorer|Chrome|Firefox|Safari|Opera}.
I'm not sure if this is a {format|formatting} issue or something to do with 
{web browser|internet browser|browser} compatibility but I {thought|figured} I'd
post to let you know. The {style and design|design
and style|layout|design} look great though! Hope you get the {problem|issue} 
{solved|resolved|fixed} soon.

{Kudos|Cheers|Many thanks|Thanks}|
This is a topic {that is|that's|which is} {close to|near to} my heart... 
{Cheers|Many thanks|Best wishes|Take care|Thank you}! {Where|Exactly where} are 
your contact details though?|
It's very {easy|simple|trouble-free|straightforward|effortless}
to find out any {topic|matter} on {net|web} as compared to {books|textbooks}, as 
I found this {article|post|piece of writing|paragraph} at this {website|web site|site|web page}.|
Does your {site|website|blog} have a contact page?
I'm having {a tough time|problems|trouble} locating it but, I'd
like to {send|shoot} you an {e-mail|email}. I've got some {creative ideas|recommendations|suggestions|ideas} 
for your blog you might be interested in hearing. Either way, great {site|website|blog} 
and I look forward to seeing it {develop|improve|expand|grow} over time.|
{Hola|Hey there|Hi|Hello|Greetings}! I've been {following|reading} your {site|web site|website|weblog|blog} 
for {a long time|a while|some time} now and finally got the {bravery|courage} to go ahead 
and give you a shout out from {New Caney|Kingwood|Huffman|Porter|Houston|Dallas|Austin|Lubbock|Humble|Atascocita} {Tx|Texas}!
Just wanted to {tell you|mention|say} keep up the {fantastic|excellent|great|good}
{job|work}!|
Greetings from {Idaho|Carolina|Ohio|Colorado|Florida|Los angeles|California}!

I'm {bored to tears|bored to death|bored} at work so I decided to {check out|browse} 
your {site|website|blog} on my iphone during lunch break. I {enjoy|really like|love} 
the {knowledge|info|information} you {present|provide} here and can't wait to
take a look when I get home. I'm {shocked|amazed|surprised} at how {quick|fast} 
your blog loaded on my {mobile|cell phone|phone} .. I'm not even using WIFI, just 3G .. 
{Anyhow|Anyways}, {awesome|amazing|very good|superb|good|wonderful|fantastic|excellent|great} 
{site|blog}!
|
Its {like you|such as you} {read|learn} my {mind|thoughts}!
You {seem|appear} {to understand|to know|to grasp} {so much|a lot} {approximately|about} 
this, {like you|such as you} wrote the {book|e-book|guide|ebook|e book} in it 
or something. {I think|I feel|I believe} {that you|that you simply|that you just} 
{could|can} do with {some|a few} {%|p.c.|percent} to {force|pressure|drive|power} 
the message {house|home} {a bit|a little bit}, {however|but} {other than|instead of} 
that, {this is|that is} {great|wonderful|fantastic|magnificent|excellent} blog. 
{A great|An excellent|A fantastic} read. {I'll|I will} {definitely|certainly} be back.|
I visited {multiple|many|several|various} {websites|sites|web sites|web pages|blogs} 
{but|except|however} the audio {quality|feature} for audio songs {current|present|existing} 
at this {website|web site|site|web page} is {really|actually|in fact|truly|genuinely} 
{marvelous|wonderful|excellent|fabulous|superb}.|
{Howdy|Hi there|Hi|Hello}, i read your blog {occasionally|from time to time} and 
i own a similar one and i was just {wondering|curious} if you get a lot of spam 
{comments|responses|feedback|remarks}? If so how do you {prevent|reduce|stop|protect against} 
it, any plugin or anything you can {advise|suggest|recommend}? I get so much 
lately it's driving me {mad|insane|crazy} so any {assistance|help|support} is very 
much appreciated.|
Greetings! {Very helpful|Very useful} advice {within this|in this particular} 
{article|post}! {It is the|It's the} little changes {that make|which will make|that produce|that will make} 
{the biggest|the largest|the greatest|the most important|the most significant} changes. 
{Thanks a lot|Thanks|Many thanks} for sharing!|
{I really|I truly|I seriously|I absolutely} love {your blog|your site|your website}.. 
{Very nice|Excellent|Pleasant|Great} colors & theme. Did you {create|develop|make|build} 
{this website|this site|this web site|this amazing site} yourself? Please reply back 
as I'm {looking to|trying to|planning to|wanting to|hoping to|attempting to} create 
{my own|my very own|my own personal} {blog|website|site} and {would like to|want to|would love to} 
{know|learn|find out} where you got this from or {what the|exactly what the|just what the} 
theme {is called|is named}. {Thanks|Many thanks|Thank you|Cheers|Appreciate it|Kudos}!|
{Hi there|Hello there|Howdy}! This {post|article|blog post} {couldn't|could not} 
be written {any better|much better}! {Reading through|Looking at|Going through|Looking through} 
this {post|article} reminds me of my previous roommate! He {always|constantly|continually} 
kept {talking about|preaching about} this. {I will|I'll|I am going to|I most certainly will} 
{forward|send} {this article|this information|this post} to him. {Pretty sure|Fairly certain} 
{he will|he'll|he's going to} {have a good|have a very good|have a great} read. 
{Thank you for|Thanks for|Many thanks for|I appreciate you for} sharing!|
{Wow|Whoa|Incredible|Amazing}! This blog looks {exactly|just} like my old one! It's on a 
{completely|entirely|totally} different {topic|subject} but it has pretty much the same 
{layout|page layout} and design. {Excellent|Wonderful|Great|Outstanding|Superb} 
choice of colors!|
{There is|There's} {definately|certainly} {a lot to|a great deal to} {know about|learn about|find out about} 
this {subject|topic|issue}. {I like|I love|I really like} {all the|all of the} points 
{you made|you've made|you have made}.|
{You made|You've made|You have made} some {decent|good|really good} points there. 
I {looked|checked} {on the internet|on the web|on the net} {for more info|for more information|to find out more|to learn more|for additional information} 
about the issue and found {most individuals|most people} will go along with your views 
on {this website|this site|this web site}.|
{Hi|Hello|Hi there|What's up}, I {log on to|check|read} your {new stuff|blogs|blog} 
{regularly|like every week|daily|on a regular basis}. Your {story-telling|writing|humoristic} 
style is {awesome|witty}, keep {doing what you're doing|up the good work|it up}!|
I {simply|just} {could not|couldn't} {leave|depart|go away} your {site|web site|website} 
{prior to|before} suggesting that I {really|extremely|actually} {enjoyed|loved} 
{the standard|the usual} {information|info} {a person|an individual} {supply|provide} 
{for your|on your|in your|to your} {visitors|guests}? Is {going to|gonna} be {back|again} 
{frequently|regularly|incessantly|steadily|ceaselessly|often|continuously} {in order to|to} 
{check up on|check out|inspect|investigate cross-check} new posts|
{I wanted|I needed|I want to|I need to} to thank you for this {great|excellent|fantastic|wonderful|good|very good} 
read!! I {definitely|certainly|absolutely} {enjoyed|loved} every {little bit of|bit of} it. 
{I have|I've got|I have got} you {bookmarked|book marked|book-marked|saved as a favorite} 
{to check out|to look at} new {stuff you|things you} post

Handling Web.Config Variants in Source Control-based Solutions

April 24, 2013

If several developers work on the same projects or solutions, sooner or later you need to manage the .config files of these developers (e.g. for local databases vs. team database, local log files and directories, and so on).

Of course, if you use a source control system, such as TFS or SVN, you don’t want the personal settings of one developer to overwrite the settings of another one.

Separate SCS config files

Usually my approach is to not check in the app.config or web.config files, but instead create copies of these files as config templates (e.g. web.tfs.config, app.svn.config) and have these copies under source control.

Recently I saw a big obstacle to this approach, and it is called TFS Team Build, an implementation of Continuous Integration.

I definitely wanted to avoid having to check out the config files, rename them so as to match the CI backend, build, and then revoke the check out.

User-specific config files

Next try, have app.config and web.config in separate sub-directories of each project, and simply copy the developer-specific app.config to app root before compilation.

While this works for app.config, it does not work for web.config in cases that the web.config contains an <authentication> element. Then the error is raised

Error 3 It is an error to use a section registered as allowDefinition=’MachineToApplication’ beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS.

(Yes, I encountered this error message before, and in that case you should be able to solve the problem by running Clean on the project or the solution)

Include user-specific config files using configSource

Next try, move all developer-specific (or installation-specific) config sections to separate .config files and include these .config section files using configSource.

When you’re done, your web.config will look something like this:

<configuration>
  <configSections>
  ...
  </configSections>
  <log4net configSource="Config\log4net.config" />
  <appSettings configSource="Config\appSettings.config" />
  <connectionStrings configSource="Config\connectionStrings.config" />
  <system.serviceModel>
    ...
    <client configSource="Config\system.serviceModel.client.config" />
  </system.serviceModel>
</configuration>

Copy the original contents of the web.config to the new files in the Config directory, e.g.:

<?xml version="1.0"?>
<connectionStrings>
    <add name="default" 
        connectionString="Data Source=localhost;Initial Catalog=cat;Persist Security Info=True;User ID=userid;Password=pwd" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

The Config directories then further contains one directory for each developer or installation environment.

To activate one of these configurations, simply copy the config files of one directory into the Config directory, but do not check-in the changes in the Config directory itself.

You can also write a batch file to perform the copy statements and invoke the .cmd from Solution Explorer.

The only drawback I see with this solution is that you lose Intellisense with these partial config section files.

 


Follow

Get every new post delivered to your Inbox.