Archive for January 2010

ASP.NET MVC CheckBoxList

January 26, 2010

This is a very simple implementation of a check box list that we use rather frequently and which hasn’t let us down (yet).

First thing to do is to write a class inheriting from Dictionary<T, bool> for our multiple choice select list, something like the following:

public class CheckedList<T> : Dictionary<T, bool>

The idea is quite simply having a boolean value per each entity we want our users to be able to select. The dictionary also allows us not to worry about duplications…

then we add an HtmlHelper extension method:

public static class HtmlHelperExtensions
 public static string CheckBoxList<T, TModel, TValue, TText>(this HtmlHelper<TModel> helper, string listName, string valueProperty,
 string textProperty, CheckedList<T> checkedList, Func<T, TValue> value, Func<T, TText> text)
 StringBuilder builder = new StringBuilder();
 for (int i = 0; i < checkedList.Count; i++)
 T item = checkedList.Keys.ToList()[i];
 builder.AppendLine(helper.Hidden(listName + "[" + i + "].Key." + valueProperty, value(item)).ToHtmlString());
 builder.AppendLine(helper.Hidden(listName + "[" + i + "].Key." + textProperty, text(item)).ToHtmlString());
 builder.AppendLine(helper.CheckBox(listName + "[" + i + "].Value", checkedList[item]).ToHtmlString());
 return builder.ToString();

This extension method could be refactored so that it’ll take Expression<Func… , that way we could extract the names of the value and text properties.

What’s left now to do is simply call the extension method from our View like so:

<%= Html.CheckBoxList("MyCheckedList", "Id", "Name", Model.MyCheckedList, x=>x.Id, x=>x.Name) %> 

Needless to say, this is only a demo and serves just to show what a check box list can be achieved; for more features and more possibilities, you could implement your very own IDictionary<T, bool>, that way you could extend and add whatever you need.

noice 🙂


Seeing that the post seem to get quite a few hits and finds itself on the first page of a google search for the terms ‘mvc checkbox list’, I thought I should provide a sample project on github. Let me know if there are any problems.


Every planet has a north

January 22, 2010

I had a truly interesting exchange of thoughts/ideas about the state of the ALT.NET community with Richard Fennell whose based in Yorkshire, England. The reason why I turned to him in the first place was to hear from him how things are in the north of England, in an attempt to draw a crude comparison between the English north and the French one (where I’m living, not working).

Richard wrote back referring me to Ian Cooper’s post entitled Whither ALT.NET? which really opened my eyes on some subjects. I wrote back a rather long email which I felt it expressed so well what I feel that I thought might as well post it on my blog (just as he did) for future reference, so here goes:


First of all let me begin by saying I only wish I could tell you how much I am thankful. Reading Ian’s post was a something of an epiphany moment 🙂 At some points he brought it so close to home that I had to stop and think ‘hold on, is he just talking about software development or is there a hidden message about the state of France..?’ Over here it’s not only the IT industry that breeds this sort of position holders that are fine where they are and just won’t bother changing anything. I always think of it as ‘with all that revolution going on, you don’t get any evolution’; the idea is that everyone here are jumping to their feet and straight to the street to cry against whatever change that is offered, that nothing ever gets to change hence no evolution…
To get back to the issue in question, I think one of the things Ian, and for that matter many of the ALT.NET

people, tend to forget or simply overlook is the fact that while at some parts of the world people may think the battle was won, or that it’s about time to wake up from our comfortable twitter hibernation, in some other parts the battle hasn’t even began, which brings me back to my original question. See, you guys up the in England and esp. in the north can be very proud of your community, and not only the development/IT one, but also the local-geographical community. I had to go and look for a job in Paris, which entails a couple of hours on the train each and every day and which is bound to end by leaving Lille; Not only developers and managers are afraid of anything that is not microsoft, the actual idea of software craftsmanship is an abnormality in our region. There is a Nord-agile group that works here and have meetings every couple of months and consists of 5 to 7 people, none of them a .net person. And we’re talking about a huge region and one of france’s 5 biggest cities.
With that in mind, there’s also the fact that roughly each and every year a new generation of developers is arriving to the market which makes it even more difficult to those (esp the beginners to senior-juniors) who wants to learn and work on their coding craftsmanship. (I remember I discovered the manifest only a couple of years ago or so, and soon after I remember reading a post of Ayende saying he’s going to give Twitter a shot. Thank god, he’s one of those who never stopped blogging.)

As for Paris, things seem to be closer to what Ian said; there are a lot more job offers that ask for a working experience in NH, MVC, NUnit etc’, however this feels like the new orthodoxy.


Just as an after note- I’ve just seen that Ian’s post has already started to have an effect. Rob conery has apparently written a post in reply which IMO completely misses the point Ian was trying to make, still it’s great to see that people are still debating 🙂

Substring and an In clause with NH Projections

January 10, 2010

This is something I’ve stumbled upon and couldn’t really find where did it all went wrong.

I have a Where clause containing a substring and an In clause. In T-Sql it looks like this:

 SELECT * FROM Employee WHERE SUBSTRING(Code, 1, 2) In ('one', 'two', 'three', 'four', 'five');

I’ve been trying to do this in NHibernate using ICriteria & Projections and came up with this code:

ICriteria criteria = session.CreateCriteria(typeof (Employee));

IProjection postCodeSubstring = Projections.SqlFunction
 ("substring", NHibernateUtil.String, Projections.Property
 Projections.Constant(1), Projections.Constant(2));

List<string> deptsIds = new List<string> {"one", "two", "three",
 "four", "five"};
 criteria.Add(Restrictions.In(postCodeSubstring, deptsIds));

IList<Employee> employees = criteria.List<Employee>();

However, this code generates some faulty t-sql:

SELECT this_.Id as Id0_0_, this_.FirstName as FirstName0_0_, this_.LastName as LastName0_0_
FROM [Employee] this_ WHERE substring(this_.FirstName, @p0, @p1)
in (@p2, @p3, @p4, @p5, @p6);
@p0 = 1, @p1 = 2, @p2 = 1, @p3 = 2, @p4 = 'one', @p5 = 'two', @p6 = 'three', @p7 = 'four', @p8 = 'five'

As you can see, the first two parameters of the In clause are the ints 1 and 2, instead of the varchars ‘one’ and ‘two’. On top of that 2 supplementary parameters were generated (@p7 = ‘four’, @p8 = ‘five’).

For the time being, the only way I found to resolve this issue is a workaround- mapping another property containing a substring formula. My FluentNH map contains these two lines now:

Map(x => x.FirstName);

Map(x => x.SubName).Formula("substring(FirstName, 1, 2)");

And the criteria would be:

ICriteria criteria = session.CreateCriteria(typeof (Employee));</pre>
List<string> deptsIds = new List<string> {"one", "two", "three", "four", "five"};
criteria.Add(Restrictions.In("SubName", deptsIds));

IList<Employee> employees = criteria.List<Employee>();

If I figure out what went wrong there with the projections, I’ll let the world know :).

Cannot bulk load because the file could not be read.

January 1, 2010

This is an extremely silly problem that may occur while trying to run a bulk insert non-query in T-SQL. The reason I’ve decided to add a post on the issue is that for some reason google didn’t have the answer for it…

So, using the example provided in msdn, I have a .dat file and an .Fmt one; when I run the following:

BULK INSERT myTestFormatFiles
 FROM 'C:\insert.dat'
 WITH (FORMATFILE = 'C:\insertFormat.fmt');

I get the following error- Cannot bulk load because the file “C:\insertFormat.fmt” could not be read.

What I needed to do was to add a new empty row at the end of the fmt file, just go to the end of the file- end of the row:

4       SQLCHAR       0       100     "\r\n"   4     Col4         SQL_Latin1_General_CP1_CI_AS

– and press enter.

Stupid, isn’t it? Just as an after note, you should also keep an eye open on the row ending character issues (“\r\n”, “\n”) and bulk inserting with files containing double quotes.