Inserting a Null Value for Integer columns in Windows Azure Table

This post will focus on providing null values for integer columns.

There may be a requirement when you need to insert null values for values type columns in an Azure table. In our recent project, we had a requirement to insert null values for integer columns. Usually if you don't provide any value to Integer variable, it would get initialized as 0, since they are value type's variables. This post will focus on providing null values for integer columns

First ,you need to define an entity as below,

WinAzrtbl1.gif

You may have noticed that I am making integer properties as Nullable types

WinAzrtbl2.gif

Now create an Azure table as below,

WinAzrtbl3.gif

Then you can insert a null value for the integer columns as below,

WinAzrtbl4.gif

For your reference, the full source code to insert a null values is below,

using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {          
            CloudStorageAccount account = CloudStorageAccount.Parse("connectionString");
            CloudTableClient tableClient = new CloudTableClient(
                                          account.TableEndpoint.ToString(),
                                           account.Credentials);          
            string tableName = "School";
            tableClient.CreateTableIfNotExist(tableName);

          
            TableServiceContext  context = tableClient.GetDataServiceContext();
            SchoolEntity entity = new SchoolEntity
                                   {
                                       Age = null,
                                       Name = "DJ",
                                       PartitionKey = "S",
                                       RollNumber  = null ,
                                       RowKey = Guid.NewGuid().ToString()
                                   };           

            context.AddObject(tableName, entity);
            context.SaveChanges();

            Console.WriteLine("Saved");
            Console.ReadKey(true);                     

          }
    }

    public class SchoolEntity : TableServiceEntity
    {
        public string Name { get; set; }
        public int ? RollNumber { get; set; }
        public int ? Age { get; set; }
    }

}

In this way you can insert null values. I hope this post was useful. Thanks for reading.