# Thursday, 19 February 2009

Stuart posted a great solution he came up with for writing dynamic orderby queries in LINQ when you don’t know the “column” name that you want to sort by ahead of time.

Thursday, 19 February 2009 13:38:59 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

I’m going to be teaching two new one-day courses on setting up a Continuous Integration process, one using freely available tools, and one using Microsoft Team Foundation Server.  For more details and to register, see our registration page.

Both classes will be held at the Westside campus of OIT

Course Description:

This one day course will provide a hands on look at setting up a Continuous Integration process using freely available tools from beginning to end. Students will learn the theory and practice behind establishing a Continuous Integration process, including setting up a build script, running unit tests, setting up an automated build/test server, and capturing reporting information for the whole process.

Course Technology

This course uses Microsoft Windows XP, the Microsoft .NET 3.5 Framework, NAnt, NUnit, and CruiseControl.NET.

At the end of the course you will be able to:

  • Create a build script using NAnt or MSBuild
  • Create and run unit tests using NUnit
  • Set up and run an automated build using CruiseControl.NET
  • Capture reporting data from the automated CI process

Course Outline
    Continuous Integration in Theory
    • Why CI is important
    • How CI fits into the Software Development process
    Creating a build script
    • What goes in a build script?
    • How does NAnt work?
    • How does MSBuild work?
    • Creating build scripts for NAnt or MSBuild
    • Running an automated build
    Adding unit tests to your build
    • Writing NUnit tests
    • Running tests as part of a build
    • Capturing test results
    Continuous Integration with CruiseControl.NET
    • Installing and configuring CC.NET
    • Adding projects to a build server
    • Reporting with CC.NET
    • Running multiple builds on the same server
        Dependencies between builds


This class is intended for experienced .NET 2.0 software developers.

Thursday, 19 February 2009 12:37:26 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, 18 February 2009

I was having a bit of trouble getting validation to work during data binding in SL, but that seems to be resolved now.  I’ve got a little time tracking app, and want to be able to log new time entries…


I want the user to be able to enter hours only in 1/4 hour increments.  The way that gets handled in Silverlight is different from WPF.  To make it work in Silverlight the data binding source has the throw an exception if the new value is unacceptable.  In this case, the source is a “LogEntry” class that has a property “Hours” that implements INotifyPropertyChanged to support full two-way data binding.

public class LogEntry : INotifyPropertyChanged

    private decimal hours;
    public decimal Hours 
            return hours;
            if ((value % .25M) != 0)
                throw new Exception("please enter time in quarter-hour increments");
            if (hours != value)
                hours = value;


    private void NotifyPropertyChanged(string p)
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(p));

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;


In the property setting for the Hours property, if the value isn’t evenly divisible by .25 an Exception gets thrown.  In the XAML, the binding is set up to notify (as a binding error) when an exception gets thrown


<TextBox Width="Auto" x:Name="txtNewHours"  Text="{Binding Hours, 
    Converter={StaticResource hoursConverter}, 
    NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>

The grid that holds all the controls handles the BindingValidationError event, which is where all the fun stuff happens…

private void newEntryGrid_BindingValidationError(object sender, ValidationErrorEventArgs e)
    if (e.Action == ValidationErrorEventAction.Added)
        //not valid
    if (e.Action == ValidationErrorEventAction.Removed)
        //now it's valid

OK, so now I know when the value isn’t valid, but what to do about it.  In WPF, the easiest thing to do would be to use a “decorator” to add a border around the UI element that failed validation, but in Silverlight we don’t get decorators.  Instead, I wrapped each item that needed to support validation in a red border with a thickness of 0 in XAML.

<Border BorderBrush="Red" BorderThickness="0">
    <TextBox x:Name="txtNewHours" Text="{Binding ...}"/>

That’s a little extra work that I’d rather not do, but there it is.  It would be easy to wrap up this behavior in a custom control, but this was much quicker.

In the validation handler then, all we have to do is get the original source of the binding error and use the VisualTreeHelper to get it’s parent.  If that parent happens to be a border, set the border’s thickness to 1 to make it visible.  I’m also setting the tooltip for that offending control to be the message of the validation error.  If the ValidationErrorEventAction == Removed, then get rid of the tooltip and set the border’s thickness back to 0.

private void newEntryGrid_BindingValidationError(object sender, ValidationErrorEventArgs e)
    if (e.Action == ValidationErrorEventAction.Added)
        DependencyObject ui = e.OriginalSource as DependencyObject 
        DependencyObject parent = VisualTreeHelper.GetParent(ui);
        if (parent is Border)
            ((Border)parent).BorderThickness = new Thickness(1);
        ToolTipService.SetToolTip(ui, new TextBlock() { Text = e.Error.Exception.Message });
    if (e.Action == ValidationErrorEventAction.Removed)
        DependencyObject ui = e.OriginalSource as DependencyObject 
        DependencyObject parent = VisualTreeHelper.GetParent(ui);
        if (parent is Border)
            ((Border)parent).BorderThickness = new Thickness(0);
        ToolTipService.SetToolTip(ui, null);


That same code should work for any control that I want to do validation on, as long as I remember to add the borders in XAML. 

Wednesday, 18 February 2009 14:10:45 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, 13 February 2009

In the wake of our Silverlight presentations this week, Shaun (CTO of SoftSource, and my boss) is going to be blogging on his demos, starting with Silverlight/JavaScript interactivity.  His demos were pretty cool, so I’m looking forward to reading his commentary.  I might just follow suit with a few examples too…

Friday, 13 February 2009 12:16:28 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

Check out http://events.sftsrc.com/ for more new classes…

  • LINQ
    • 2 days of LINQ to Objects, LINQ to XML, LINQ to SQL, LINQ to Entities and more
    • 5 days of ASP.NET including some AJAX and a hint of Silverlight
  • WPF
    • 3 days of WPF goodness
  • Silverlight
    • 2 days for those with WPF experience or
    • 3 days for web developers
  • C#  and the .NET Framework
    • 5 days of C# 3.0 and the 3.5 framework, including LINQ, extension methods and the rest of the new language features
Friday, 13 February 2009 10:17:36 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, 06 February 2009

Google is great at providing 20/20 hindsight. 

The little problem I was having with insert triggers is (unsurprisingly) not uncommon.  I googled a bit after a suggestion in a comment (thanks El Guapo) and found a great explanation by Mike Taulty on how to deal with just such a concurrency problem.  He’s specifically dealing with a timestamp column, but the issue is essentially the same. 

Good to know…

Friday, 06 February 2009 09:39:15 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, 05 February 2009

I’ll be there, soaking up the Silverlight-y goodness. 

Thursday, 05 February 2009 14:56:38 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

Lets say I have a nice simple database (guess which one…)


Now I want to insert a new Order into the Orders table


using (NorthwindDataContext context = new NorthwindDataContext())


    Order order = new Order();




No problem.  Totally works like you’d think.  What happens if I make more changes after that and call SubmitChanges() again?

using (NorthwindDataContext context = new NorthwindDataContext())


    Order order = new Order();




    order.Freight = 98.77M;

    order.ShipName = "Fred Jones";




Still cool.  The problem comes in if I add an insert trigger.  Let’s say I add a trigger to set the OrderDate column of the order to make sure it’s always the right date…


ON dbo.Orders




SET OrderDate = GETDATE()

FROM Orders INNER JOIN inserted ON Orders.OrderID = inserted.OrderID

Whether or not that’s a good idea is a whole ‘nother topic.  Seemed like the thing to do at the time…  However, now there’s a problem.  The first SubmitChanges() works fine, but the second one fails with an update conflict.  WTF?  After I stopped to think about it (and talked to Stuart) I realized that that makes perfect sense.  The copy of Order that I have in memory doesn’t know anything about the result of the insert trigger, and so the record is out of date, hence the conflict.  In this particular case it wasn’t a problem because it wasn’t too much work to just take out the trigger and set the date in the code.  I had been hoping that I wouldn’t have to redeploy the web app, hence the trigger.  Best laid plans…

Anyway, it turns out that something similar came up on another project, so it is something to be aware of.

Thursday, 05 February 2009 14:49:23 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [2]  | 

After reading Scott’s latest missive, I finally gave in and joined the Tweeple.


Thursday, 05 February 2009 13:04:23 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, 22 January 2009
Yesterday I posted a bit about a dynamic survey in Silverlight, and today I wanted to start delving into a few details.  First of all, databinding to a user control…

I wanted my datagrid to show a series of radio buttons representing a “rating” from 1-5, where that rating corresponds to an enum value in C# and an integer in SQL. 


For databinding to work the way I’d want, I need to bind the one value in the source to the set of radio buttons in the target.  The easiest way I could think of (and there may be a better way I haven’t thought of) was to make the radio buttons into a user control, and expose a single value.  In the XAML for the datagrid, it looks like



        <t:Rating UserRating="{Binding Path=Answer, Mode=TwoWay}" />



The CellTemplate contains just the user control, and binds its UserRating property to the Answer property in the source.

The XAML for the UserControl is simple

<UserControl x:Class="Evaluation.Rating"


   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <StackPanel Orientation="Horizontal">

        <RadioButton Content="Excellent (5)" x:Name="excellent" Checked="excellent_Checked"/>

        <RadioButton Content="Good" x:Name="good" Checked="good_Checked"/>

        <RadioButton Content="Neutral" x:Name="neutral" Checked="neutral_Checked"/>

        <RadioButton Content="Adequate" x:Name="adequate" Checked="adequate_Checked"/>

        <RadioButton Content="Poor (1)" x:Name="poor" Checked="poor_Checked"/>



In order to get two-way databinding notifications to work properly both ways, the UserRating property of the control is implemented as a DependencyProperty…

public ScaledAnswer UserRating


    get { return (ScaledAnswer)GetValue(UserRatingProperty); }

    set { SetValue(UserRatingProperty, value); }



// Using a DependencyProperty as the backing store for UserRating.  This enables animation, styling, binding, etc...

public static readonly DependencyProperty UserRatingProperty =

    DependencyProperty.Register("UserRating", typeof(ScaledAnswer), typeof(Rating), new PropertyMetadata(new PropertyChangedCallback(RatingChangedCallback)));

Note the callback registered for the PropertyChanged event of the dependency property.  If databinding changes the value of the dependency property, we still need to update the UI to check the right radio button.

private static void RatingChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)


    Rating r = d as Rating;





private void updateUi(ScaledAnswer rating)


    switch (rating)


        case ScaledAnswer.Excellent:

            excellent.IsChecked = true;


        case ScaledAnswer.Good:

            good.IsChecked = true;


        case ScaledAnswer.Neutral:

            neutral.IsChecked = true;


        case ScaledAnswer.Adequate:

            adequate.IsChecked = true;


        case ScaledAnswer.Poor:

            poor.IsChecked = true;




Finally, the user control handles all the radio button “Checked” events and sets the value of the DependencyProperty to the correct value.

The UserControl provided a fairly smooth way of handling the databinding without having to implement a custom control, and could be reused in other contexts.  There’s probably a way to make it a bit more dynamic to handle more radio buttons, etc. but this worked for my particular case.

Thursday, 22 January 2009 12:54:28 (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |