Programming and Technology blog

Some fun at the Winnipeg Code Camp 2012

Overall the Winnipeg Code Camp 2012 did not disappoint, lots of interesting sessions to be found and even more interesting people.

One cool item was: Sam - the Twitter Aware, Arduino Powered, Traffic Light.  Tyler & Jay talk about Sam:

These were the following sessions I took in, each of them offered something different and were quite enjoyable.

  • ASP.NET MVC vs. Ruby on Rails SMACKDOWN - ROUND 2! (Part 1) (Marc Jeanson & James Chambers)
  • ASP.NET MVC vs. Ruby on Rails SMACKDOWN - ROUND 2! (Part 2) (Marc Jeanson & James Chambers)
  • Git 101: Source Control for the Future (David Alpert)
  • Software Craftsmanship Panel Discussion (David Alpert, Amir Barylko, James Chambers, and others!)
  • Illuminated Integration with Team City and Arduino (Tyler Dueck & Jay Smith)

P.S. the winner of the smack down was RoR by a significant margin.  Both sessions and presenters were very informative and dove into the how each framework handles certain features and "how do you do this" in each framework.

Just want to give a big kudos to the organizers and speakers for putting on another successful code camp as well the folks over at Skullspace for hooking up free Wi-Fi for everyone in attendance.

Type 'ContosoUniversity.DAL.Department' could not be found

So I was going through the tutorial Handling Concurrency with the Entity Framework in an ASP.NET MVC Application when I encountered this error on step 7 of 10.

Type 'ContosoUniversity.DAL.Department' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 1, column 132.

The error is thrown on the following line:

var databaseValues = (Department)entry.GetDatabaseValues().ToObject();

This error is due to a bug in EF 4.1 where you separate the DAL and Model Entities out into different namespaces and is referenced here EF4.1 CodeFirst - entry.GetDatabaseValues() throw EntitySqlException.

To fix this issue I came up with this workaround:

Replace the lines:

var databaseValues = (Department)entry.GetDatabaseValues().ToObject(); 
var clientValues = (Department)entry.Entity;

with the following lines:

var clientValues = (Department)entry.CurrentValues.Clone().ToObject();
var databaseValues = (Department)entry.CurrentValues.ToObject();

It seems to work ok for simple concurrency checks and it allows you to continue on with the tutorial.

Converting a DataTable to a Html table


This post is another common question I see on the ASP.NET forums "How to convert a DataTable to a Html table".  The code uses simple looping through the DataTable’s columns and rows/columns to pull the column names and the row data out.  I use the HtmlTable, HtmlTableRow and HtmlTableCell objects to actually build the table on the fly.  I find this much easier than using a StringBuilder to create the Html.

The code samples in this post can be downloaded using the link at the bottom and were created using Visual Studio 2010.  There are samples for both C# and Visual Basic.NET in the download file as well. (24 kb)

T4 Template lovin'

The following is a couple of T4 templates that I have been fooling around with in my spare time (sharing the time between jQuery, StringTemplate view engine for ASP.NET MVC and other endeavors).  For the most part these templates are nothing special, they generate both the T-SQL (stored procedures) and a simple but effective data access layer.  Like I said nothing special.

A good resource for starting with T4 templates is @ David Hayden site where he has a Screencast on how to use them.  Below describes some of the settings used to get my simple sample up and running.
Extract the, and files from the zip and add them to your VS project in a folder (like Generation).  In the file there are some settings (variables) that you can use to modify how the code is generated.  You will need SQL 2005 client tools installed on your machine as it uses the SMO object library.  Once the code is compiled just cut and paste it into separate class files and run the T-SQL code on your db.

  • ConnectionString - This is your connection string to your database
  • TableName - This is the table you are going to run the code against
  • SchemaName - Use this for applying a schema to your T-SQL code if you use schemas (default is dbo)
  • ColumnsToOmit - Comma delimited list of columns to omit from the DAL code that is generated
  • NameSpace - Namespace of your application
  • ProcPrefix - Use to add a prefix to your stored procedures (ie. up_)
  • UseShortProperties - True | False to tell code generation to generate the C# short form for get/set properties
  • insertColumnsAsGetdate - Comma delimited list of column names that will automatically be assigned the GETDATE() T-SQL statement on an Insert.  (Also omits the fields from the insert statement parameters)
  • updateColumnsAsGetdate - Comma delimited list of column names that will automatically be assigned the GETDATE() T-SQL statement on an Update.  (Also omits the fields from the update statement parameters)

The code is supplied as is and if it's broken fix it, you're a programmer aren't you?  :P

T4 (4.98 kb)

Introducing The AgeSpan Class

Recently I was working on a web service that required logic than needed the span of time passed in months and days based on a start and end date.  I figured someone has already written this before me so why waste my time.  EPIC WRONG!!!!!!  While I was able to find code (mostly snippets) that did various calculations most of them were either; a) not what I needed or b) fundamentally flawed in the calculations (most choked on month/day crossovers, where the as-of-date was 1 month previous and 1 day later than the sample start date).  So I decided that I would take a chapter from the TimeSpan class and create my own AgeSpan class.

This class helps answers the question how many years, months, days, total months, total days from the birth date to the as-of-date.  So for example on Mar 04 2009 this class tells me I am 40 years; 5 months and 1 day old or 485 total month or 14762 total days old.

One of the options available is to set the include as of date in the days calculation.  What this does is include the as of date inclusively as part of the date range.  Basically it adds one day to the days and total days.  So taking the example from above I would be 40 years; 5 months and 2 days old or 485 total month or 14763 total days old.  This option is set to false by default.

Included in the zip is a unit test project to validate the actual logic.  The class is written in c# and it probably can be ported to VB pretty easily or just use the assembly as is.

AgeSpan Class

Download the code: (521.59 kb)