Reading data from a database using a DataReader is fast and easy. If something goes wrong, mostly that has to do with null values.
To prevent that, you can extend the DataReader class with some handy methods.

The example below uses the MySqlClient, however, you can use the same methods for SQL and OleDb.
Also, the example only shows methods for integers and strings. Of course you can write similar methods for all data types.


using System;

namespace Utilities
{
    public static class Extentions
    {

        public static int SafeGetInteger(this MySql.Data.MySqlClient.MySqlDataReader reader, string colName)
        {
            int result = 0;
            if (reader[colName]!= null)
            {
                string tmp = reader[colName].ToString();
                if (int.TryParse(tmp, out result))
                {
                    return result;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
            
        }

        public static string SafeGetString(this MySql.Data.MySqlClient.MySqlDataReader reader, string colName)
        {
            if (reader[colName] != null)
                return reader[colName].ToString();
            else
                return string.Empty;
        }
    }
}

There are many ways to convert a boolean to text. The most simple one is by using the .ToString() method.

Let’s look at the following code:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool answer;

            answer = (DateTime.Now.Year == 2016);

            
            Console.WriteLine("Yes or no: the current year is 2016?");
            Console.WriteLine("The answer is: {0}", answer.ToString());

            Console.ReadLine();
        }

     }
}

The output will be “The answer is: true” (or false). That’s not what you want, you want the answer to be Yes or No.

You could try something like this:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool answer;

            answer = (DateTime.Now.Year == 2016);

            
            Console.WriteLine("Yes or no: the current year is 2016?");
            Console.WriteLine("The answer is: {0}", answer ? "Yes" : "No");

            Console.ReadLine();
        }
     }
  }

And that works just fine. However, what if you have multiple places where you want to convert a boolean to “Yes” or “No”? You would have to write that code everytime you do the conversion.

And what if you have added this 100 times, and decide afterwards that you do not want “Yes” and “No”, but “Yeah!” and “Nope…”?

For that, the most practical way is using an extention method.

By doing so, you can add functionality to a boolean (and it works on other types as well, in fact, it works on all types!). This is how that’s done:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool answer;
            answer = (DateTime.Now.Year == 2016);
            
            Console.WriteLine("Yes or no: the current year is 2016?");
            Console.WriteLine("The answer is: {0}", answer.ToYesNo());

            Console.ReadLine();
        }
   }

    public static class Extentions
    {
        public static string ToYesNo(this bool b)
        {
            return (b ? "Yes" : "No");
        }
    }
}

All you have to do is add a top-level static class to your project (it can be placed in a separate file, or added to other code, you only need to add it once in your project. The name of that class can be anything, but it makes more sense if you give it a logical name, like “Extentions”.

In that class, you put a public static method, with the return type of your choice (a string in this case). The trick is that you give that method a parameter with the prefix “this“, followed by the type you want to extend (a bool in this case). Give the parameter a name (b).

In the example above, the type bool has a new method: .ToYesNo(). When calling the method, omit the parameter. Simple and effective!

Imagine what you can do with that! Just for fun:


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sum = "10+10";
            
            Console.WriteLine("The outcome of {0} is {1}",sum, sum.CalcSum());

            Console.ReadLine();
        }
   }

    public static class Extentions
    {
        public static int CalcSum(this string s)
        {
            int ret = 0;
            string[] p = s.Split('+');
            ret = int.Parse(p[0]) + int.Parse(p[1]);

            return ret;
        }
    }
}

EnJoy!