Using KnockoutJS with SignalR in ASP.NET MVC

KnockoutJS is a MVVM implementation for JavaScript written by Steve Sanderson, in my opinion the author of the best ASP.NET MVC textbooks available. Simply put it lets you bind a JavaScript object model to your HTML UI using a Read more

A MongoDB Tutorial using C# and ASP.NET MVC

In this post I'm going to create a simple ASP.NET MVC website for a simple blog that uses MongoDB and the offical 10gen C# driver. MongoDB is no NOSQL database that stores information as Binary JSON (BSON) in documents. I Read more

Linq To SQL Tutorial

Check out some of my other Linq to SQL posts: EntityBase Inheritance Modifiers with SQLMetal Linq to SQL with WCF Services Linq to SQL Framework (Repository/Business wrapper) ObjectDataSource binding with server side paging and sorting Load Options Generic Framework using reflection This is a basic tutorial for Read more

Using the jQuery UI Datepicker with Fancybox

Posted on by Joe in jQuery | 3 Comments

I just wasted what seems to be far too much of my life (about 30 minutes), trying to figure out why the jQuery UI Datapicker wouldn’t work when it was applied to a text box within a Fancybox.

It turns out it was working, but it was being placed behind the Fancybox which had it’s z-index set to 1101.

Adding the following CSS worked a treat

#ui-datepicker-div
{
    z-index: 1102 !important;
}

ASP.NET MVC authorize attribute using action parameters with the ActionFilterAttribute

Posted on by Joe in C#, MVC | 1 Comment

ASP.NET MVC provides the AuthorizeAttribute which ensures there is a logged in user. You can also provide parameters to restrict actions or controllers to only be accessible to certain roles or users. You can also create your own custom authorization attribute derived from AuthorizeAttribute to provide any custom authorization.

In addition to this general authorization you may want to restrict access based on the current user and a parameter from the action. For example, say you have an action method to edit the details of a product.  You would pass the ID of the product to the action method, and you may only want certain users to be able to edit this particular product.  The AuthorizeAttribute doesn’t allow you to do this but you can create your own attribute derived from ActionFilterAttribute which gives you the desired result.

Read more

Linq lambda expression IEqualityComparer for IEnumerable.Distinct and Except

Posted on by Joe in C#, Linq | 3 Comments

One of the things that annoys me with the IEnumerable.Distinct method is that it has no overload allowing you to give a lambda expression to specify a particular property to perform the distinction, you have to give an IEqualityComparer.

I did a quick Google search and found this post. The guy here provides the following LambdaComparer class:

Read more

ASP.NET MVC Textbox with characters remaining HtmlHelper extension method

Posted on by Joe in C#, JavaScript, MVC | 5 Comments

Here is a couple of HtmlHelper extension methods, CharactersRemainingTextBoxFor and CharactersRemainingTextAreaFor which render a textbox/textarea with a span tag that displays the number of characters remaining.  They also include the JavaScript to make this work, and to limit the number of characters entered in the textarea. The maximum limit comes from the model by using the StringLengthAttribute.

Download source

Read more

WordPress automatic upgrade timeout

Posted on by Joe in Personal | Leave a comment

I was just trying to upgrade my wordpress blog to version 3 using the automatic upgrade but it kept stopping after 30 seconds while unpacking the update. I checked on my server and it hadn’t completed the unpacking.  I loaded up the PHP.ini file and after a look around found the max_execution_time setting which was set to 30 seconds. I increased this and the update worked fine.

Dell Warranty Ripoff

Posted on by Joe in Personal | 2 Comments

About 15 months ago I bought a new Dell Studio 17; with the added options is cost me around $3,500. I thought I paid extra for an additional 12 month warranty on top of the standard 12 months but when I rang recently to get a faulty hard drive replaced it turned out my warranty had expired.

The guy on the phone went through to sales to get me a quote for a new 12 month warranty. He came back with a “discounted” price of $700, with some sales spiel about how with this warranty my “hard drive problem would definately be rectified”. When I told him that was ridiculous and that I could fix it myself he backtracked and said they could get the price down as that cost included all sorts of other crap I didn’t ask for… not so much Mr Dell.

I ended up buying a new 500GB 7,200rpm Seagate drive for around $100, swapping it over myself in about 5 minutes, then used Acronis Home to image my failing drive onto the new one to avoid having to reinstall everything (which I doubt Dell would have done).

It irritates me that Dell try this kind of BS as people who don’t know better would just pay. I guess the fact that I also bought a Dell Netbook just a couple of months ago doesn’t count for anything either.

How to convert a JSON date serialized by an ASMX web service to a JavaScript Date object

Posted on by Joe in Ajax, ASP.NET, JavaScript | 5 Comments

JSON doesn’t have a standard way to represent a date. You can read about the reasons behind this here.

If you are using an ASMX web service returning JSON then you’ll find it serializes the DateTime object to a string that looks like this:

/Date(1278943200000)/

The numer in this string is the number of milliseconds since January 1st 1970 UTC, and this number can be used as a constructor argument to the JavaScript Date object.  So all we need to do is extract the number of milliseconds from the string.  This can be done easily using the regular expression shown in the post linked above.

var dateString = "/Date(1278943200000)/";
var date = new Date(parseInt(dateString.replace(/\/Date\((\d+)\)\//, '$1')));

In the above example I’ve manually set the date string, but you may get this from an AJAX response from the ASMX web service.  I then create a new Date object by using the regular expression and parsing the result at an integer.

How to get the time difference between two DateTimes using C#

Posted on by Joe in C# | 3 Comments

Here’s a quick an easy way to show the time difference between two DateTime objects using C#.

The DateTime structure has an overridden subtract operator which return a TimeSpan object when subtracting two DateTimes:

public static TimeSpan operator -(DateTime d1, DateTime d2);

You can then use this TimeSpan to get the amount of time between each DateTime:


DateTime fromDate = DateTime.Now.AddDays(-1).AddMinutes(-10).AddSeconds(-20);
DateTime toDate = DateTime.Now;
TimeSpan timeSpan = toDate - fromDate;

string timeString =
    string.Format(
        "{0:00}:{1:00}:{2:00}",
        (timeSpan.Days * 24) + timeSpan.Hours,
        timeSpan.Minutes,
        timeSpan.Seconds
    );

In the above example I’m comparing a date in the past to the current date. This could be useful for working out the total amount of time that has passed since something has happened.

I’m then using the Hours, Minutes and Seconds properties of the TimeSpan to format a string displaying the amount of time. If the hours goes over 24 then the Days property is used, so for my hours I’m also adding on the number of days multiplied by 24.

Convert a comma separated string of numbers to an integer array using C#

Posted on by Joe in C#, Linq | 1 Comment

Today I needed to convert a comma separated string of numbers in an integer array. Here is how you can do it in one line using Linq:

string csv = "1,1,2,3,5,8,13,21,34";
int[] numbers = csv.Split(',').Select(n => int.Parse(n)).ToArray();

Running MSTest on your build server without Visual Studio

Posted on by Joe in Visual Studio 2008 | Leave a comment

Recently I had a problem where I added a Visual Studio Test Project to a solution.

As part of our CI, the build server failed to build as it didn’t contain the necessary files for the test framework. One option would have been to install Visual Studio Team System on the build server, but it seems a bit crazy to do that just to get the tests to build.

I found a post here by Mark Kharitonov that explains a method to get tests building without having to install Visual Studio which worked perfectly for me.