Friday, 7 December 2007

LINQ to Everything

Last month, as I was leaving the excellent .NET Developer Network in Bristol, I was considering the various data sources that can be queried using LINQ. There's LINQ to XML, LINQ to objects, LINQ to SQL and so on. As I was just about to leave building, I walked past a sign showing the words "Link to P Block". And suddenly, everything seemed all right with the world.

Technorati Tags: LINQ

Tuesday, 20 November 2007

How to Test Code that Generates Email

If you're writing code that generates emails, you'll probably want to test it on real addresses, external to your office. One solution I've found useful in the past is to use one of the temporary email generators. Try or

Saturday, 10 November 2007

Reflections of a Week at Tech Ed Developers

Got back home from Barcelona at about 2 am this morning. It's been an interesting week in many ways:
  • Flew out on the Sunday, having arrived at the airport just 10 minutes before the check-in desk closed (they closed the M4, resulting in massive delays and forcing us into detours and a few wrong turns)

  • White-knuckle ride in the taxi from the airport to the hotel... felt just like we
    were living in a computer game!

  • Clean hotel within easy walking distance of the conference centre (but why don't
    Spanish hotels have tea-making facilities in the rooms?)

  • Attended some very interesting and useful sessions, including:

    • debugging in a production environment (ie. without Visual Studio)

    • hidden gems in ASP.NET 2.0

    • how to make phones ring in code

    • continuous integration

    • top 10 mistakes developers make

    • XNA game development

    • embedded development (.NET on a chip!)

    • how to make your T-SQL code a gazillion times more efficient in certain scenarios

    • extreme XSLT

  • Took rather too much advantage of the free bar at the country drinks night and had to miss the first session the following day as a result (whoops)

  • Won a remote-controlled helicopter

  • Bagged some swag (including four t-shirts, a stack of magazines, various software
    goodies, a good book on the security development lifecycle, and, of course, the delegate bag)

  • Through the week, actually got to hold some real conversation in Spanish

  • Flight on the way home delayed due to wranglings between the airline and the Spanish immigration authorities over a deported a passenger

On the whole, it was a very worthwhile and genuinely useful event. However, I couldn't help noticing differences between this year's event and the one I attended in 2005. There was less of a wow factor this time (check out the 2005 video, if you can find a copy, to see what I mean). There were also fewer giveaways and leaner facilities (most noticeable was the lack of VOIP, an overall event party and key Microsoft software). I presume these changes are due to the developer event now being separate from the IT professional event and therefore the budget having to be spread more thinly. Additionally, I preferred the old format of four days, plus a pre-conference day. This year, it felt like a four-and-a-half-day event.

One welcome improvement over the 2005 event is the introduction of proper videos of the sessions. These are likely to be a very useful resource and more engaging than screencasts.

Adios amigos.

Thursday, 11 October 2007

Generating Custom Configuration Section in web.config

The following tool on dmitryr's blog generates the code to implement a custom configuration section in web.config:

It also generates the schema to give you IntelliSense support.

Not yet used it myself, but looks like it could be useful.

[Edit 30/1/2008: I’ve added a new post detailing how to implement custom sections yourself without needing to use a tool. See:]

Wednesday, 4 July 2007

How to Set the Maximum Length of a MultiLine TextBox

The MaxLength property of a TextBox control has no effect when the TextMode is set to MultiLine. The reason for this is that a MultiLine TextBox is rendered as a <textarea> tag, which does not have a maximum length attribute:

<textarea name="txtComments" rows="2" cols="20" id="txtComments">

In contrast, a TextBox with its TextMode set to SingleLine is rendered as an <input> tag, which has the maxlength attribute:

<input name="txtComments" type="text" maxlength="5" id="txtComments" />

If you wish to restrict the amount of text in a MultiLine TextBox, the workaround is to use a RegularExpressionValidator control. In the following example, the validation expression allows between 0 and 5 alphanumeric characters to be entered:

string strMaxLength = "5";

revComments.ValidationExpression = "\\w{0," + strMaxLength + "}$";

Sunday, 1 July 2007

Always Use Response.End() After FormsAuthentication.RedirectToLoginPage()

Here's something else that wasn't immediately apparent to me:

If you’re using FormsAuthentication.RedirectToLoginPage() to direct unauthenticated users to a login page, you must follow that statement with a Response.End() if the page would otherwise continue loading content. Although the code appears to branch, the redirect does not stop the execution of the rest of the page.

Redirects appear to work fine in a browser (and would pass all UI tests). They issue an “object moved” status, causing the browser to reload a new page. However, this reload relies on the client’s browser taking this action. If you inspect the response (eg. using telnet), you'll see that when browsing to a protected page, which has the redirect in the Page_Load handler, the “object moved” flag is correctly set, but the content following FormsAuthentication.RedirectToLoginPage() continues to be output, completely negating the purpose of log-in.

The solution is to add Response.End() after the redirect. The output is then correctly terminated as the following telnet session shows (click on the image to view it in full):

Screen shot of a command line telnet session

Do Not Use Underscores in Host Names

As part of a recent development task, I’d written some code to log users in to an extranet. It worked fine in Firefox, but when used in IE, it kept redirecting users back to the login screen. Debugging showed that users were being successfully authenticated, but that this was being lost as soon as the user tried to navigate to another page. After a long and painful process and the enlisting of help from colleagues, the problem was traced to an underscore in the host name (ie. the hosts file had an entry similar to “”). Is was only half chance that the culprit was discovered—we tried to add a host header in IIS and it complained about invalid characters.

It took about three days of project time and much hair-pulling to pin down this obscure issue. One to note!