May 15, 2015

All these methods Int32.Parse(), Convert.ToInt32(), and Int32.TryParse() ultimately does the same thing - convert a string to an integer. The main difference lies in what happens if the conversion does not succeed. With Int32.Parse(), exceptions are thrown if conversion does not work. With Convert.ToInt32() methods 0 is returned while parsing null, otherwise exceptions are thrown. With Int32.TryParse() method, no exception is thrown - it sets the correct value to an integer variable if conversion succeeds and at the same time returns a boolean value suggesting if conversion has succeeded.


February 18, 2015

Another new feature of C# 6.0 is Exception Filters which allows catching all exceptions except for some. We can basically write an if statement before entering the catch block meaning that we can and cannot enter the catch block. For example, we can catch all exceptions except for say ArithMatic exception or we can check the value of a property before entering the catch block.

Here is an example of how this would work.

private void GetData()
        //do something
    catch(Exception ex)
    if(ex.GetType() != typeof(ArithmeticException) && ex.GetType() != typeof(SqlException) && ex.InnerException != null)
        //handle exception
        //do something

Read on other new features of C# 6.0.

One of the new features introduced in C# 6.0 is Conditional Access Operator. This really means that we can use the ? and ?? operators while checking if a property of an object exists. Previously, we had to write if(object != null) and so on but now there is a shorthand of writing this using the conditional operators.

Here's an example of how it works.

private string GetName(NewStuff stuff)
    string ans = "";
    //old way
    if (stuff != null && stuff.Name != null)
        ans = stuff.Name.ToUpper();
        ans = "no name";

    //new way
    ans = stuff?.Name? .ToUpper() ?? "no name";

    return ans;

Read on other new features of C# 6.0.

February 17, 2015

C# 6.0 has introduced a new feature that allows specifying Static Classes in the using statement. For example, previously we would have written System.Console.WriteLine("some message") or Console.WriteLine("some message") but now we can just write WriteLine("some message") and specify the class name in the using statement. Note that the syntax has recently changed from using StaticClass to using static StaticClass. Here's an example of how to use the static class.

using System;
using static System.Console;
using static System.Math;


public static void LogMessage(string message)

public static void GetArea(int radius)
    //the old way
    Console.WriteLine(Math.PI * Math.Pow(radius, 2));

    //the new way
    WriteLine(PI * Pow(radius, 2));


As part of the example, just note how much less typing we would need while using static.

Read on other new features of C# 6.0.

This is a new feature in C# 6.0. There is no syntax improvement but a feature improvement and this is new feature allows await keyword to be called within the catch section of a try-catch block. This allows for async exception handling when async results in an exception.

Here is an example of how to use the code.

  1. //await within a catch block
  2. async void DoSomething()
  3. {
  4.     try
  5.     {
  6.         NewStuff stuff = new NewStuff();
  7.         Task<bool> task = stuff.BuggyMethod();
  8.     }
  9.     catch (Exception ex)
  10.     {
  11.         await Logger.Log(ex.StackTrace);
  12.     }
  14. }

Read on other new features of C# 6.0.

With the Auto Property initialization feature, properties can be initialized without using private setter or a local variable. Previously, we used to set the properties within the constructors, with C# 6, the properties can be initialised within the properties' get-set section. A constructor is not required.

Here is an example of how this can be achieved.

Property Initializer
  1. class NewStuff
  2. {
  3.     public long ID { get; } = -1;
  4.     public string Name { get; } = "John";
  5.     public string Email { get; protected set; } = "";
  6. }
  8. class OldStuff
  9. {
  10.     private string email = "";
  11.     public OldStuff()
  12.     {
  13.         //initialiser
  14.         ID = -1;
  15.         Name = "John";
  16.     }
  18.     public long ID { get; set; }
  20.     public string Name { get; set; }
  22.     public string Email
  23.     {
  24.         get { return email; }
  25.         protected set
  26.         {
  27.             email = value;
  28.         }
  29.     }
  30. }


Read on other new features of C# 6.0.

January 30, 2015

I have a website that I configured to use https using IIS and have setup the links to point to the https version of the site. However, IIS does not automatically redirect the http version of the site to https and instead display error messages on the screen. This is bad as other sites had linked to the http version of prior to setting up https.

January 8, 2015

HTML - Pattern Matching

From HTML5, a pattern attribute has been introduced which allows validation of an input field using regular expression. The validation is carried out by the browser and no javascript is required. This is great news for developers as sometimes javascript validation becomes a tedious and repetitive task.

In this post, I will show how to use patterns in html forms with examples and then list a few common regular expression. Note that, in the example, I have not included a regular expression for email address. It's actually not required to validate email using regular expression and instead we can directly use the input type as email.

In this post, I will show how to read a single file from a password protected zip file and then display it on a web browser. This scenario might be more required in a windows application but since the focus is to read from the zip file than to display it on the web browser, I will go ahead with browser.

To read from the zip file, I am using a NuGet package - SharpZipLib. This library can ne used to read a password protected zip file. If you are not familiar on how to add or manage NuGet packages, then check the NuGet Docs. After the package is added, I will use the ICSharpCode.SharpZipLib.Zip to read the zip file.

December 30, 2014

Load XML in a GridView using Asp.Net

Xml files can be loaded into a GridView control in To load xml files, it can be usually binded to the GridView by creating the xml data source and then setting the data source property of the GridView. However, if xml files contains nodes, then it does not work and instead produces error -

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

To bind the xml file with nodes, it's easier to create a dataset and then to bind it to the gridview from code behind. Here is an example of how that would work.

Sample Xml File
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Customers>
  3.   <Customer>
  4.     <Id>C1</Id>
  5.     <Name>John</Name>
  6.     <DateOfBirth>9 Jan 1960</DateOfBirth>
  7.     <Phone>99999999</Phone>
  8.     <Address>Canberra</Address>
  9.   </Customer>
  10.   <Customer>
  11.     <Id>C2</Id>
  12.     <Name>Smith</Name>
  13.     <DateOfBirth>10 August 1980</DateOfBirth>
  14.     <Phone>88888888</Phone>
  15.     <Address>Sydney</Address>
  16.   </Customer>
  17.   <Customer>
  18.     <Id>C3</Id>
  19.     <Name>Peter</Name>
  20.     <DateOfBirth>12 June 1978</DateOfBirth>
  21.     <Phone>77777777</Phone>
  22.     <Address>Melbourne</Address>
  23.   </Customer>
  24. </Customers>


aspx page
  1.     <div>
  2.     <asp:GridView ID="GridView1" runat="server">
  3.     </asp:GridView>
  4. </div>


Code to bind xml to gridview
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.     if (!IsPostBack)
  4.     {
  5.         DataSet ds = new DataSet();
  6.         ds.ReadXml(MapPath("~/App_Data/data.xml"));
  7.         GridView1.DataSource = ds;
  8.         GridView1.DataBind();
  9.     }
  10. }
Reference: Shahed Kazi at