Dynamic Column Management In Azure Table Storage With ASP.NET MVC And C#

This article will give a step-by-step demonstration of how to manage Dynamic Column or Field Operations in Azure Table Storage by using ASP.NET MVC and C#.

Introduction

 
This article will demonstrate how to manage Dynamic Column in Azure Table Storage. In the previous article, I have given an idea of how to store value in Azure Table Storage. Dynamic Column generation is not a new requirement in a real-life project. Here, I’m giving you the procedure to manage this by Azure Table Storage.
 

Overview

 
Azure Storage is a Microsoft managed service providing cloud storage that is highly applicable, scalable, and redundant. Azure Storage includes Blobs, Tables, Queues, and Files.
 
Azure Table Storage is a service that stores unstructured No-SQL data in the cloud, providing a key/attribute store with a scheme-less design. Access to table storage data is fast and cost-effective for many types of applications and is typically lower in cost than traditional SQL for similar volumes of data.
 

Concept

 
Because Azure Table Storage is NoSql, we should create a dynamic column in this Storage mechanism. Suppose a user wants to create a new column, they don’t need to contact the developers. This should be the flavor of No-SQL.
 

Consideration

 
Before starting this, the following skills should be considered.
  • Good knowledge of ASP.NET MVC 5
  • OOPs concepts in C#
  • Basic knowledge of NoSQL
  • Azure Table Storage Basic Concept (here)
To create this project, I’m using.
  • .Net Framework 4.6
  • Windows Azure Storage 8.1.4 package (Nuget)
  • Visual Studio 2015
  • MVC 5.0

Scope of Work (SOW)

 
A Registration form will be developed where fields or columns are dynamic, and user created.
  1. Column Manage screen – In this screen user can create columns for registration. 

    Dynamic Column manage in Azure Table Storage with ASP.NET MVC and C#

  2. Registration Screen – In this screen, the Registration Form will be generated by a user-defined column.

    Dynamic Column manage in Azure Table Storage with ASP.NET MVC and C#
Step 1
 
Create an Azure Storage Account. From Azure Portal.
 
Access Key & Connection String
 
After creating Azure Storage Account, go to that account and copy the access key and connection string of that account. It will be used in our MVC application.
 
Step 2
 
Create a new empty MVC application. The step to create a new project is File -> New -> Project…
 
Go to NuGet package manager console window and install -> “Install-Package WindowsAzure.Storage -Version 8.1.4”.
 
Step 3
 
Go to my previous article of Azure Table Storage. You can see a class, “TableManager” there. Use this class for accessing the Azure table.
 
Add a model, “DataModel”, for managing Master Property or Column Data. 
  1. public class DataModel : TableEntity  
  2. {  
  3.     public string ColoumnName { getset; }  
  4.     public string DataType { getset; }  
  5. }  
Add a screen in the MVC application with Property Name as ColoumnName and Data Type (here is used, string, number and bool) to set Master Property data.
 
Note
There are some rules to create a Dynamic Column Name
  1. The name should be in alpha only
  2. White spaces or special characters are not allowed 
Step 4
 
Add two methods in the class “TableManager” to insert and Retrieve Dynamic Column Data
  1. public void InsertEntity(DynamicTableEntity entity)  
  2. {  
  3.     try  
  4.     {  
  5.         var insertOperation = TableOperation.Insert(entity);  
  6.         table.Execute(insertOperation);  
  7.     }  
  8.     catch (Exception ExceptionObj)  
  9.     {  
  10.         throw ExceptionObj;  
  11.     }  
  12. }  
  13. public List<DynamicTableEntity> RetrieveEntity(string Query)  
  14. {  
  15.     try  
  16.     {  
  17.         TableQuery<DynamicTableEntity> DataTableQuery = new TableQuery<DynamicTableEntity>();  
  18.         if (!string.IsNullOrEmpty(Query))  
  19.         {  
  20.             DataTableQuery = new TableQuery<DynamicTableEntity>().Where(Query);  
  21.         }  
  22.         IEnumerable<DynamicTableEntity> IDataList = table.ExecuteQuery(DataTableQuery);  
  23.         return IDataList.ToList<DynamicTableEntity>();  
  24.     }  
  25.     catch (Exception ExceptionObj)  
  26.     {  
  27.         throw ExceptionObj;  
  28.     }  
  29. }  
Step 5
 
For the registration page add an HTTPGET action result into MVC application to populate dynamic column, where user can insert data.
  1. [HttpGet]  
  2. public ActionResult Registration(string id)  
  3. {  
  4.     TableManager tablePropMaster = new TableManager("PropMaster");  
  5.     ViewBag.Tenant = id;  
  6.     List<DataModel> ModelList = tablePropMaster.RetrieveEntity<DataModel>("PartitionKey eq '" + id + "'");   
  7.     return View(ModelList);  
  8. }  
Step 6
 
In the view screen of Registration add this HTML code. 
  1. @using (Html.BeginForm("Registration", "Home",  
  2.      new { id = ViewBag.Tenant },  
  3.      FormMethod.Post,  
  4.      new { autocomplete = "off",   
  5. enctype = "multipart/form-data",   
  6. data = "data-parsley-validate",   
  7. @class = "form-horizontal"   
  8.    }))  
  9. {  
  10.     <h2>Registration</h2>  
  11.     foreach (DataModel dm in Model)  
  12.     {  
  13.         <div class="form-group">  
  14.             <label for="firstName" class="col-sm-3 control-label">@dm.DisplayColoumnName</label>  
  15.             <div class="col-sm-12">  
  16.                 @if (dm.DataType == EdmType.String.ToString())  
  17.                     {  
  18.                     <input type="text" placeholder="@dm.DisplayColoumnName" name="@dm.ColoumnName" class="form-control">  
  19.                 }  
  20.                 else if (dm.DataType == EdmType.Int32.ToString()  
  21.                     || dm.DataType == EdmType.Int64.ToString()  
  22.                     || dm.DataType == EdmType.Double.ToString())  
  23.                 {  
  24.                     <input type="number" placeholder="@dm.DisplayColoumnName" name="@dm.ColoumnName" class="form-control">  
  25.                 }  
  26.                 else if (dm.DataType == EdmType.Boolean.ToString())  
  27.                 {  
  28.                     <select class="form-control" name="@dm.ColoumnName">  
  29.                         <option value="true">True</option>  
  30.                         <option value="false">False</option>  
  31.                     </select>  
  32.                 }  
  33.             </div>  
  34.         </div>  
  35.     }  
  36.     <button type="submit" class="btn btn-primary">Register</button>  
  37. }  
Step 7
 
On submitting the registration form add a POST method to insert the registration data.
  1. [HttpPost]  
  2. public ActionResult Registration(FormCollection form, string id)  
  3. {  
  4.     TableManager tablePropData = new TableManager("PropData");  
  5.     TableManager tablePropMaster = new TableManager("PropMaster");  
  6.     DynamicTableEntity dte = new DynamicTableEntity();  
  7.     List<DataModel> ModelList = tablePropMaster.RetrieveEntity<DataModel>("PartitionKey eq '" + id + "'");  
  8.     EntityProperty Prop;  
  9.     foreach (DataModel dm in ModelList)  
  10.     {  
  11.         if (dm.DataType == EdmType.String.ToString())  
  12.         {  
  13.             Prop = new EntityProperty(form[dm.ColoumnName]);  
  14.             dte.Properties[dm.ColoumnName] = Prop;  
  15.         }  
  16.         if (dm.DataType == EdmType.Int32.ToString()  
  17.             || dm.DataType == EdmType.Int64.ToString()  
  18.             || dm.DataType == EdmType.Double.ToString())  
  19.         {  
  20.             Prop = new EntityProperty(Convert.ToDouble(form[dm.ColoumnName]));  
  21.             dte.Properties[dm.ColoumnName] = Prop;  
  22.         }  
  23.         if (dm.DataType == EdmType.Boolean.ToString())  
  24.         {  
  25.             Prop = new EntityProperty(Convert.ToBoolean(form[dm.ColoumnName]));  
  26.             dte.Properties[dm.ColoumnName] = Prop;  
  27.         }  
  28.     }  
  29.     dte.PartitionKey = id;  
  30.     dte.RowKey = Guid.NewGuid().ToString();  
  31.     tablePropData.InsertEntity(dte);  
  32.     return RedirectToAction("Registration");  
  33. }  
Step 8
 
Here is the code to retrieve the dynamic column value.
  1. TableManager tablePropData = new TableManager("PropData");  
  2. List<DynamicTableEntity> DataList = tablePropData.RetrieveEntity("PartitionKey eq 'Microsoft'");  

Error Handling

 
You can handle the error by Storage Exception class. Find the error list here.