Tuesday, December 30, 2008

Add RSS Feeds in 2 seconds

Hi All, 

Just a short post to show you how to add RSS to your website in 2 easy steps.

Step 1 : Create an XML file in your solution and add a link to it from your page.

Step 2 : Use this format to make RSS posts 

Hope this helps, 

 - Tim 

Dynamically Load Controls onto Page

Hi All, 

I was recently asked to dynamically load usercontrols onto a page based on what other controls were on the page. Here is how you do it :



Hope this helps, 

 - Tim

Becoming your own Contracting Agency

Hi All, 

Recently, I have considered starting my own contracting agency, so I would not have to always go through a recruitment agency for new contracts. Seeing that I have recently had an influx of contracts thrown my way, I thought it was time to start looking at having my own business where I could contract under my own company name. There is one problem, there really isn't a lot of information out there to help with exactly what I wanted. After some snooping around, some networking and talking to colleages I have composed this for anyone considering the same avenue.

There seems to be 4-5 ways of approaching the idea and they can be summarised into

1. Going through a Recruiter
2. Rent-A-Coder Approach
3. Your own Company
4. Management Company
5. Cough...Cough...Under the table approach.

Going through a recruiter.
The advantages of going through a recruiter include services that are often neglected when taking on a contracting job. This includes the management of Tax, Super, Payroll, Insurance and other HR/Employments needs. Sure you pay a bit of money to the recruitment agency, but in return you get the assurance, that all you have to do is your job and you will get paid.

Rent a Coder Approach
This approach is based on the idea that you get work off sites like www.rentacoder.com and simply set up a bank account that business pay money into. There are pitfalls with this approach. 

A) You aren't guareenteed any work
B) I have personally been stung with this before, and was not paid for my work
C) You may need to diversify your tech skills to keep this as a steady stream of revenue.

Your Own Company approach
Let's face it, every developer would like to answer the question "What do you do for a living?" with "oh, I own insert company name here, an IT contracting company"......right? Some of the advantages of this approach include

A) Tax breaks
B) You are your own boss - could be bad if you are not motivated
C) Work from home
D) Acquire a reputation - could also be bad :)
E) Work on new products all the time
F) Great hourly rates
G) Easier to avoid work place "politics"
H) Easy to walk away after a contract.

However with all the advantages come many disadvantages being

A) You have to organise everything....paperwork, insurance, contracts, super etc.
B) No paid leave
C) Usually pay for your own training
D) Work locations can vary

Management Company
There are plenty of companies out there (not recruitment) that will do all the management for you (super, insurance etc) however the only difference is that they will not find you jobs. If you are the kind of person that finds a lot of work through friends, people in the industry, then i would definitely recommend this. They do not charge as much as recruitment agencies and from my research, do just as good a job. Some agencies within Australia include :

Under the table approach
In no way do I condone tax dodges...if anything I simply suggest people do them :)

This approach bsaically involves working with people you know, people you have network with for less money, however in return you are payed in cash.

Hope this helps, 

 - Tim

Sunday, December 28, 2008

My Brothers Web Site

Hi All, 

In the short time off I had over Christmas (very busy) I was able to find a few hours to build my brothers new web site. He wanted something simple, something stylish and something easy for him to maintain. Ben (my Brother) is a creative graphic artist and required something to send around as a resume. Here is what a couple of bored hours over Christmas produced :

If anyone wants, I can put up the code for all the jQuery etc. (Just leave a comment)


 - Tim

Sunday, December 7, 2008

Bad Characters in XML

I was recently asked by a friend, "How to ignore bad characters in an XML file?" Two ways of doing it include, using XSLT to transform the document to only use nodes that have good characters in it, or you can simply use the tags around the characters that are of a different encoding standard and the XMLDoc object will ignore it.

Creating Custom HTTP Handlers

Hi all, 

I am now at the stage in my project where I need to find ways of fine tune the loading times. Of course I have used tools like Firbug etc to run diagnosis on what is taking a long time to load etc. One thing I did find, was that a lot of the time I was using .ASPX files to load something that really only need a method call, and not all the overhead that comes with an ASPX page. So what else to use, but a custom HTTP Handler!

I decided that I would use a ASHX file to load my CSS into the pag
e instead of just adding a reference within the ASPX page and making .net do all the work. The payoff, was a load time of the CSS that was about half!!!!

Here is how you do it.

Create a Generic Handler file in your Website or Web Application.

Call the file "CSSHttpHandler"

You will be presented with the following code :

By default you only have to implement one method and a property when implementing the IHttpHandler interface

public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    public bool IsReusable {
        get {
            return false;

Replace the code with this :

using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO.Compression;

public class CSSHttpHandler : IHttpHandler {

    private const string PrefixAllImagesWith = "~/";
    private readonly static TimeSpan KeepInCache = TimeSpan.FromDays(15);
    private readonly static Regex UrlCheck = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.Compiled);

    public void ProcessRequest(HttpContext context)
        context.Response.ContentType = "text/css";
        var themeName = context.Request["theme"];
        var themeFileNames = context.Request["files"];
        var version = context.Request["version"];

        var isCompressed = CanGZip(context.Request);

        var encoding = new UTF8Encoding(false);

        if (WriteFromCache(context, themeName, version, isCompressed)) return;
        using (var memoryStream = new MemoryStream(5000))
            using (var writer = isCompressed ? (Stream)(new GZipStream(memoryStream, CompressionMode.Compress)) : memoryStream)
                if (!string.IsNullOrEmpty(themeName))
                    var themeCssNames = themeFileNames.Split(',');
                    foreach (var fileName in themeCssNames)
                        var fileBytes = GetCss(context,
                                                  "~/App_Themes/" + themeName + "/" + fileName,
                                                  PrefixAllImagesWith, version, encoding);
                        writer.Write(fileBytes, 0, fileBytes.Length);


            var responseBytes = memoryStream.ToArray();
            context.Cache.Insert(GetCacheKey(themeName, version, isCompressed),
                                 responseBytes, null, System.Web.Caching.Cache.NoAbsoluteExpiration,

            WriteBytes(responseBytes, context, isCompressed);

    private static bool WriteFromCache(HttpContext context, string themeName,
        string version, bool isCompressed)
        var responseBytes = context.Cache[GetCacheKey(themeName,
            version, isCompressed)] as byte[];

        if (null == responseBytes) return false;

        WriteBytes(responseBytes, context, isCompressed);
        return true;

    private static void WriteBytes(byte[] bytes, HttpContext context, bool isCompressed)
        var response = context.Response;

        response.AppendHeader("Content-Length", bytes.Length.ToString());
        response.ContentType = "text/css";
        if (isCompressed)
            response.AppendHeader("Content-Encoding", "gzip");

        context.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");

        response.OutputStream.Write(bytes, 0, bytes.Length);

    private static bool CanGZip(HttpRequest request)
        var acceptEncoding = request.Headers["Accept-Encoding"];
        return !string.IsNullOrEmpty(acceptEncoding) &&
               (acceptEncoding.Contains("gzip") || acceptEncoding.Contains("deflate"));

    private static byte[] GetCss(HttpContext context, string virtualPath,
        string imagePrefix, string version, Encoding encoding)
        var physicalPath = context.Server.MapPath(virtualPath);
        var fileContent = File.ReadAllText(physicalPath, encoding);

        var imagePrefixUrl = imagePrefix +
        var cssContent = UrlCheck.Replace(fileContent,
            new MatchEvaluator(delegate(Match m)
                var imgPath = m.Groups["path"].Value.TrimStart('\'').TrimEnd('\'').TrimStart('"').TrimEnd('"');

                if (!imgPath.StartsWith("http://"))
                    return "url('" + imagePrefixUrl 
                        + imgPath 
                        + (imgPath.IndexOf('?') > 0 ? "&version=" + version : "?version=" + version)
                        + "')";
                    return "url('" + imgPath + "')";

        return encoding.GetBytes(cssContent);

    private static string GetCacheKey(string themeName, string version, bool isCompressed)
        return "CssHttpHandler." + themeName + "." + version + "." + isCompressed;

    public bool IsReusable
            return true;

Then in the Render Method of the Page you are loading e.g. Default.aspx simply use this code

 var themeName = Page.Theme;
        if (string.IsNullOrEmpty(themeName)) return;
        var linksToRemove = new List();
        foreach (Control c in Page.Header.Controls)
            if (c is HtmlLink)
                if ((c as HtmlLink).Href.Contains("App_Themes/" + themeName))
                    linksToRemove.Add(c as HtmlLink);

        string themeCssNames = "";
        linksToRemove.ForEach(delegate(HtmlLink link)
                                      themeCssNames += VirtualPathUtility.GetFileName(link.Href) + ',';

        var linkTag = new Literal();

        var cssPath = CSS_PREFIX + "CssHttpHandler.ashx?theme=" + themeName
                      + "&files=" + HttpUtility.UrlEncode(themeCssNames.TrimEnd(','))
                      + "&version=" + CSS_VERSION;

        linkTag.Text = string.Format(@"&ltlink href=""{0}"" type=""text/css"" rel=""stylesheet"" /%gt", cssPath);

Hope this Helps, 

 - Tim


There are so many ways to do validation these days and all have their benefits. I have been working with the built in .net Validation Controls and from this have defined a list of tips : 

Here are the built in controls you recieve from the framework

Here is the form I am dealing with :

Required Field Validator

This control is simply used to make sure another control, such as a textbox will error if the page tries to redirect without the textbox having anything entered in it.


Set the error message as something descriptive
Set the text to "*"
Set the ControlTo Validate as the Textbox you wish to control

Range Validator

This control makes sure that the control that this validator is controlling falls within a bounds. 

Regular Expression Validator

This control uses Regular Expressions to Validate another control


There are built int Regula
r Expressions for commonly used validation types e.g. Phone Numbers, Email Address' etc.

Compare Validator

This control is used to compare to controls to each other. This a great control to use for password and confirm password controls.

Custom Validator

This control is used for complex validation that the other validation controls cannot perform. For example, if you wanted to check if the number entered in a textbox is divisble by 2.



protected void IsDivisibleByTwo(object sender, ServerValidateEventArgs e)
 // Some Code

Validation Summary

I always use this control at the bottom of the page. It basically grabs all the validation controls on the page and displays their error messages in a summary format.

Hope this helps,

 - Tim

Sunday, November 30, 2008

Enable Themes in you Web Site

I recently had to implement Themes in a Web Site that I am working on and found out that the .net framework has made it very easy to implement. Here is how I did it :

In my default.aspx page on the OnPreInit method (overridden) I simply add this :

protected override void OnPreInit(EventArgs e)
        if (Session["ThemeName"] == null)
            Session.Add("ThemeName", "Green");
            Page.Theme = ((string)Session["ThemeName"]);
            Page.Theme = ((string)Session["ThemeName"]);

Then from anywhere on the page I simply call this :

if (Session["MyTheme"] == null)
            Session.Add("ThemeName", "Black");
            if (((string)Session["ThemeName"]) == "Green")
                Session["ThemeName"] = "Black";

                Session["ThemeName"] = "Green";

Hope this helps, 

 - Tim

Thursday, October 23, 2008

Random String Generator

I encountered the problem where I needed to build a lot of controls at runtime and of course I needed the to have a name. I decided to use a Random String builder and here is what I cam up with:

 public static string RandomString(this String str, int size, bool lowerCase)
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(2 * random.NextDouble() + 28)));
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();

* One small side note is that the 2 and the 28 in the sample have no correlation to the result, I just wanted a completely random result.

Dynamically Render an Image on a Web Page ASP.net

I have used this trick so many times, I am somehow surprised that Microsoft don't have the built in functionality for rendering an image at runtime on a web page. All that needs to be done is creating a new .aspx page (we will call it RenderImage.aspx) within your project and in the Page_Load method, insert the following code.

byte[] ImageInfo = //get 
  MemoryStream s = new MemoryStream();
              s  = new MemoryStream(ImageInfo );
            catch (Exception ex)

if (s.Length != 0)
                        Response.BinaryWrite(ImageInfo) ;

Call it from another .aspx like so :

Image2.ImageUrl = "RenderImage.aspx";

Couldn't be easier.....well you say that now....just keep in mind that if you are using an UpdatePanel with ajax, that the call to BinaryWrite will not be liked, as it causes a MemoryStream error. 


Bind an Entity to a Row.DataItem

It is innevitable these days that when building a website you will need to use the GridView control within Visual Studio. In my early days of programming, I used to simply use Row[some int] to get row information on the Editing, Deleting and ItemBound events (Bad). Now that I am using the entity framework, I needed a way to be able to strongly bind an entity to a Row that I selected within the Gridview. Easy!

 public static class EntityDataSourceHelper where TEntity : class
public static TEntity GetDataItemEntity(object dataItem)
            var entity = dataItem as TEntity;
            if (entity != null)
                return entity;

            var td = dataItem as ICustomTypeDescriptor;
            if (td != null)
                return (TEntity)td.GetPropertyOwner(null);

            return null;

Calling this is simple....

      protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowIndex > -1)
                PersonEntity person=        EntityDataSourceHelper< PersonEntity  >. GetDataItemEntity(e.Row.DataItem);


This saves you from a couple of issues :

1. Referencing null column
2. Extra code to cater for null checks on rows
3. Extra code for casting etc

Tuesday, September 30, 2008

Deploying A Windows Mobile Application From Your Desktop (Vista/XP)

From my experience there seems to be very limited support for Windows Mobile Development. Sure, Visual Studios is great with its emulators, and essentially a lot of things you can do in Windows Forms, you can also do in a Windows Mobile Application, but one aspect I did not find a lot of support on was deploying CAB packets to an Active Sync'd device through an MSI package. After searching the internet for a while I came across a tool entitled, CABviaActiveSync which although did exactly what I wanted (install a CAB file from my desktop to my device), I still peronsally believe that having an MSI package is much more user friendly. It also would give me the oppurtunity to inject custom install options etc into my procedure (such as defining a Web Service IP address for my Mobile Device to Sync with).  Below is a tutorial on how to install your Windows Mobile application onto your Windows Mobile Device via your desktop.

Extension Methods Done Right in C#

Extension methods are great! In the past I would simulate Extension methods by building Bookmark folders in Visual Studios for common string functions, math functions etc. I then migrated to using code snippet tools to save little reusable snippets of code, but always found it difficult for others to know that I had implemented a new code snippet and that they could now use it in their code. But alas, along came Extension methods and all my problems were solved. I now have a great way to organise all my code snippets into a contextual structure and also make it immediately available to the rest of the developers in my team. Below is a simple example of extending the Dictionary class to implement a "SortDictionary" method.

 static class DictionaryExtensions: IDictionary
        internal static Dictionary SortDictionary(this Dictionary data)
        internal static Dictionary<double, double>  SortDictionary(this Dictionary<double, double>  data)
            List<KeyValuePair<double, double> >  result = new List<KeyValuePair<double, double> > (data);
            delegate(KeyValuePair<double, double>  x, KeyValuePair<double, double>  y)
                return y.Value.CompareTo(x.Value);
            result.ForEach(delegate(KeyValuePair<double, double> dkvp) { data.Add(dkvp.Key, dkvp.Value); });

            return data;

Now from any class in my solution I can add a reference to this project containing this Extension Class and call the functionality as follows :

 Dictionary<double, double> UnsortedDictionary = new Dictionary<double, double> ();
Dictionary<double, double>  SortedDictionary = UnsortedDictionary.SortDictionary();

Hope this helps,
 - Tim

From SQL to LINQ in C#

I am currently working on a new project using the latest technologies from Microsoft. Although most of the technologies are still in Beta, one of the languages I had to learn was LINQ. Coming from a SQL background, I could honestly say that the step from SQL to LINQ was a bit daunting. Statements that I wrote in SQL within 2 minutes took me over an hour to figure out in LINQ to start with. Below is a outline of converting SQL thinking to LINQ thinking that became the fundamental driver behind writing all my LINQ statements now.

For example, using this framework I could easily translate the following SQL call into LINQ syntax quite easily :

SQL Statement :

DECLARE @lat float
DECLARE @lng float

SET @lat = 40.66666666666
SET @lng = -3.54434333344

SELECT     b.BlockId, b.PolygonId
FROM         Block AS b INNER JOIN
                      Polygon AS p ON b.PolygonId = p.PolygonId INNER JOIN
                      Point AS po ON p.PolygonId = po.PolygonId
GROUP BY b.BlockId, b.PolygonId
HAVING (MIN(po.Latitude) < @lat) AND  (MAX(po.Latitude) > @lat) AND (MIN(po.Longitude) < @lng) AND  (MAX(po.Longitude) > @lng)

LINQ Statement : 

Entities Context = ContextManager.Instance.GetContext();
            var Query = from B in Context.Block
                                  where B.Polygon.Point.Min(point => point.Latitude) <>
  && B.Polygon.Point.Max(point => point.Latitude) > Latitude 
  && B.Polygon.Point.Min(point => point.Longitude) <>
  && B.Polygon.Point.Max(point => point.Longitude) > Longtitude
                          select B;

            return Query.FirstOrDefault();

As long as you follow the structure it seems as though most SQL statements can be easily translated to LINQ this way. One thing to note, is that I am using LINQ to Entities (Objects), and there are certain limitiation/bugs that I have encountered with using the "FirstOrDefault()" method when deailing with LINQ to SQL.

Hope this helps, 

 - Tim