Calculate a running total in SQL Server 2012

In the previous versions of SQL Server, calculating a running total for say, a bank account ledger, has been a frustratingly complex task. Fortunately SQL Server 2012 makes this a breeze with new support for windowed aggregate functions.
Continue reading

Posted in SQL | 2 Comments

Bind a model property to a different named query string field

The ASP MVC model binder automatically maps a query string to an object model by matching each field to a property name. This is very handy, but things can quickly get quite verbose:

http://domain/products?CategoryId=42&SortBy=Name&SortAscending=True

Ideally what I want is to give my class properties an abbreviated alias for use in the query string. Thus, I should be able to use the following URI instead, and still have the input values mapped automatically to the model:

http://domain/products?c=42&s=Name&asc=True

Fortunately there is an elegant solution, which I must admit I’m blatantly writing up based on an answer given on Stack Overflow. My solution does however fix a bug to make multiple aliases on a single property work as intended. Continue reading

Posted in ASP.NET MVC | Tagged , , , | Leave a comment

Grouping data with LINQ and MVC

Showing a list or table of data in groups is very common, and fortunately also very easy to do with LINQ. In the end we want our data to look something like this: Continue reading

Posted in ASP.NET MVC | Tagged , | 1 Comment

Mapping relational table data to a tree structure in MVC

With the advent of MVC in ASP.NET, proper object oriented code is encouraged more than ever, and Razor makes it a joy to work recursively with tree structures. So how do we go about converting our SQL table data to a tree structure? Continue reading

Posted in ASP.NET MVC | Tagged , | 4 Comments

Localization with fallback using LINQ

So you need to support multiple languages in your website, and you need the system to fallback to a default language if no translation is available. Fortunately LINQ is here to help and can do it all within a single SQL statement.

Since localization is a basic need for all international applications, I have made a simple structure, which can be applied in situations where an entity is required to have localizable values.
Continue reading

Posted in ASP.NET MVC | Tagged , | Leave a comment

Firefox auto login with Windows Authentication and AD

When creating intranet websites, Windows Authentication will take care of all your user identification needs. Both Internet Explorer and Chrome will automatically pass your NTLM (Active Directory) user credentials to the server, but Firefox will not and instead presents the user with an ugly login prompt. Fortunately it’s quite easy to enable this in Firefox with a simple configuration change. Continue reading

Posted in Tips | Tagged , , , | Leave a comment

View source on the iPad and iPhone

As a web developer I’m frequently looking at the source code of various websites. Lately my new iPad has become my primary tool for surfing and reading documentation, but alas it completely lacks a view source feature.

A fine solution is to create a bookmarklet, which is a piece of JavaScript saved as a bookmark. When you want to see the source of a web page, just click the bookmark and the source of the page is displayed. I was inspired by this bookmarklet by Rob Flaherty, but it has a few shortcomings. To improve upon the bookmarklet concept, I created my own version with a few more bells and whistles: Continue reading

Posted in Tools | Tagged , , , , , , | 22 Comments

Gmail+ your email – filter maillists and catch spam

Gmail by Google logoA great “hidden” feature in Gmail, is the option to append a + followed by any text you like to the username part of your email address.

This text will be ignored by Gmail, and you will receive your emails as usual, but it allows you to create unique email addresses on the fly. Continue reading

Posted in Tips | Tagged | Leave a comment

Custom MembershipProvider, Principal and Identity

Recently I ported a large website from ASP.NET Web Forms to the shining new ASP.NET MVC 3. During this process I also decided to implement “proper” MembershipProvider-based security instead of a simpler, custom HTTP module we were running.

As I started implementing my custom MembershipProvider, I became a bit confused, however, about the sheer number of classes you have to implement/override. I ended up spending quite a lot of time searching for help, so here I will attempt to give a “shortcut” overview of the structure of the MembershipProvider model and lessons learned. Continue reading

Posted in ASP.NET MVC | Tagged , , , | 6 Comments

Using URI hash instead of query strings

A URI hash is a great way to make JavaScript/AJAX pages with dynamic content bookmarkable. It can be used in a manner similar to query strings, but changes will not cause a new page request. This allows you to store data in the URI which can be read and changed by JavaScript without ever reloading the page. Continue reading

Posted in JavaScript | Tagged , , , | Leave a comment