Create Table on Cloud in Azure

Introduction

Today, in this article let's play around with one of the interesting and most useful concepts in Azure.

Question: What is creating a table in a Cloud?

In simple terms "It enables creation of a table in a Cloud in Azure client storage".

Step 1: Open Visual Studio and create an "ASP.NET Web Application", as in:

 

Output1.jpg
 

Step 2: The compete code of StudentEntity.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using Microsoft.WindowsAzure.StorageClient;

namespace CreateCloudTableApp

{

    public class StudentEntity : TableServiceEntity

    {

        public int Id { get; set;

        }

        public string FirstName

        {

            get; set;

        }

        public string LastName

        {

            get; set;

        }

        public int Age

        {

            get; set;

        }

    }

}
 

Step 3: The complete code of webform1.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CreateCloudTableApp._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="2">

                        <asp:Label ID="Label1" runat="server" Text="Create Table on Cloud Operations - Azure"

                            Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label3" runat="server" Text="Please Enter Table Name" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox1" runat="server" Width="120px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <center>

                            <div>

                                <table>

                                    <tr>

                                        <td>

                                            <asp:Label ID="Label7" runat="server" Text="Please Enter Id" Font-Size="Large" Font-Names="Verdana"

                                                Font-Italic="true"></asp:Label>

                                        </td>

                                        <td>

                                            <asp:TextBox ID="TextBox5" runat="server" Width="120px"></asp:TextBox>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                            <asp:Label ID="Label2" runat="server" Text="Please Enter First Name" Font-Size="Large"

                                                Font-Names="Verdana" Font-Italic="true"></asp:Label>

                                        </td>

                                        <td>

                                            <asp:TextBox ID="TextBox2" runat="server" Width="120px"></asp:TextBox>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                            <asp:Label ID="Label4" runat="server" Text="Please Enter Last Name" Font-Size="Large"

                                                Font-Names="Verdana" Font-Italic="true"></asp:Label>

                                        </td>

                                        <td>

                                            <asp:TextBox ID="TextBox3" runat="server" Width="120px"></asp:TextBox>

                                        </td>

                                    </tr>

                                    <tr>

                                        <td>

                                            <asp:Label ID="Label6" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"

                                                Font-Italic="true"></asp:Label>

                                        </td>

                                        <td>

                                            <asp:TextBox ID="TextBox4" runat="server" Width="120px"></asp:TextBox>

                                        </td>

                                    </tr>

                                </table>

                            </div>

                        </center>

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="text-align: center">

                        <asp:Button ID="Button1" runat="server" Text="Create Table on Cloud" Font-Names="Verdana"

                            Width="213px" BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" style="top: 50px; text-align: center">

                        <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

 

Step 4: the Complete code of webform2.aspx.cs looks like this:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.WindowsAzure.StorageClient;

using Microsoft.WindowsAzure;

using System.Configuration;

namespace CreateCloudTableApp

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            TextBox1.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text) || string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox5.Text))

            {

                Label5.Text = "";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            {

                CloudStorageAccount obj_Account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["ClientConnectionString"]);

                CreateTable(obj_Account);

                Label5.Text = "Table created successfully"; Label5.ForeColor = System.Drawing.Color.Green;

            }

        }

        protected void CreateTable(CloudStorageAccount storage)

        {

            CloudTableClient cloud_Table = new CloudTableClient(storage.TableEndpoint.ToString(), storage.Credentials);

            cloud_Table.CreateTableIfNotExist(TextBox1.Text);

            TableServiceContext table_Context = cloud_Table.GetDataServiceContext();

            StudentEntity obj_Entity = new StudentEntity();

            obj_Entity.FirstName = TextBox2.Text;

            obj_Entity.LastName = TextBox3.Text;

            obj_Entity.Age = int.Parse(TextBox4.Text);

            obj_Entity.Id = int.Parse(TextBox5.Text);

            obj_Entity.PartitionKey = obj_Entity.FirstName; obj_Entity.RowKey = Guid.NewGuid().ToString();

            table_Context.AddObject(TextBox1.Text, obj_Entity);

            table_Context.SaveChanges();

        }

    }

}

 

Step 5: The output of the application looks like this:

  Output2.png

I hope this article is useful for you.

 


Similar Articles
MVC Corporation
MVC Corporation is consulting and IT services based company.