Home > DeveloperSection > Beginner > Insert, Update, Delete in Windows Azure Application

Insert, Update, Delete in Windows Azure Application


Cloud Computing Cloud Computing 
Ratings:
2 Comment(s)
 7038  View(s)
Rate this:

Insert, Update, Delete in Windows Azure Application

In this article I am going to explain how to perform insert, update, delete and select data using ADO.NET Entity Framework in a Windows Azure application.

·         Open Microsoft Visual Studio 2010 as an administrator.

·         To create a new project FileNewProject.

·         Select Cloud template from the Installed Templates.

·         Select Windows Azure Project and enter the name of the project as DataOperation.

·         Click OK to proceed.

Insert, Update, Delete in Windows Azure Application

·         To add an asp.net web role to the solution, choose ASP.NET Web Role and then choose the right arrow. The roles are displayed in the Windows Azure solution pane of the dialog box. You can rename it as DataOperation.

·         Click OK to add.

Insert, Update, Delete in Windows Azure Application

After adding web role, delete all the files from the DataOperation web role in the solution explorer except References, Properties, and WebRole.cs.

·         Add a web form (Default.aspx) in a web role.

·         Add a new web.config file.

Modify the Default.aspx as shown below:

   <div>

        <table border="1" align="center">

            <tr><td>

                <table cellpadding="10" cellspacing="10">

                   <tr>

                      <td align="right">

                          <asp:Label ID="lblId" runat="server" Text="Employee ID:">
                          </
asp:Label>

                       </td>

                       <td>

                          <asp:TextBox ID="txtId" runat="server"></asp:TextBox>

                       </td>

                    </tr>

                    <tr>

                       <td align="right">

                          <asp:Label ID="lblName" runat="server" Text="Name :" > 
                          </
asp:Label>

                        </td>

                        <td>

                           <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

                        </td>

                    </tr>

                    <tr>

                       <td colspan="2" align="center">

                         <asp:Button ID="btnInsert" runat="server" Text="Insert"
                                                           OnClick="btnInsert_Click" />

                         <asp:Button ID="btnUpdate" runat="server" Text="Update"
                                                           OnClick="btnUpdate_Click" />

                         <asp:Button ID="btnDelete" runat="server" Text="Delete"
                                                           OnClick="btnDelete_Click" />

                       </td>

                     </tr>

                     <tr>

                       <td colspan="2" align="center">

                          <asp:GridView ID="gridEmpData" runat="server" CellPadding="10"
                                                   ForeColor="#333333" GridLines="None">

                               <AlternatingRowStyle BackColor="White" />

                               <EditRowStyle BackColor="#2461BF" />

                               <FooterStyle BackColor="#507CD1" Font-Bold="True"
                                                                 ForeColor="White" />

                               <HeaderStyle BackColor="#507CD1" Font-Bold="True"
                                                                 ForeColor="White" />

                               <PagerStyle BackColor="#2461BF" ForeColor="White"
                                                           HorizontalAlign="Center" />

                                <RowStyle BackColor="#EFF3FB" />

                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
                                                                ForeColor
="#333333" />

                                <SortedAscendingCellStyle BackColor="#F5F7FB" />

                                <SortedAscendingHeaderStyle BackColor="#6D95E1" />

                                <SortedDescendingCellStyle BackColor="#E9EBEF" />

                                <SortedDescendingHeaderStyle BackColor="#4870BE" />

                          </asp:GridView>

                       </td>

                     </tr>

                </table>

            </td></tr>

        </table>

  </div>

 

Now you have to create an ADO.NET Entity Data Model:

·         Right click on web role (Data Operation) in the solution explorer.

·         Select AddNew Item.

·         Select Data template from the Installed Templates.

·         Select ADO.NET Entity Data Model and enter the name of the model.

·         Click Add

Insert, Update, Delete in Windows Azure Application

In the next window ((Entity Data Model Wizard), select Generate from database and click Next.

Insert, Update, Delete in Windows Azure Application

Then fill the connection string properties (Server Name, User Name, Password and Database name) in the Connection Properties window and click OK.

Insert, Update, Delete in Windows Azure Application

 

When you click on OK, Entity Data Model Wizard will open. Enter the entity connection names as CloudEntities and click on Next button.

Insert, Update, Delete in Windows Azure Application

Select a table that you want to select. Here I am selecting an Employee table contains two columns i.e. Id (int) and Name (string).

Enter the model namespace as AzureModel and click on Finish button.

Insert, Update, Delete in Windows Azure Application

When you click on the Finish button, the structure of the model should something like below:

Insert, Update, Delete in Windows Azure Application

Now in the code behind of the Default.aspx (Default.aspx.cs), add the following code in order perform the insert, update, delete and select operation:

 protected void Page_Load(object sender, EventArgs e)

        {

            //display all the record present in the database first time on page load

            if (!Page.IsPostBack)

                RefreshGrid();

        }

       //display the current data from database table in a gridview

private void RefreshGrid()

        {

            GridView gridData = gridEmpData;

            gridData.AutoGenerateColumns = false;

            gridData.Columns.Clear();

            gridData.Columns.Add(new BoundField() { HeaderText = "Id", DataField = "Id"
                                                                                    });

            gridData.Columns.Add(new BoundField() { HeaderText = "Name", DataField =
                                                                              "Name" });

            CloudEntities entities = new CloudEntities();

            gridData.DataSource = entities.Employee.ToList();

            gridData.DataBind();

        }

 

        //insert an employee record in the table

        protected void btnInsert_Click(object sender, EventArgs e)

        {

            CloudEntities entities = new CloudEntities();

            Employee emp = new Employee();

            emp.Id = Convert.ToInt32(txtId.Text);

            emp.Name = txtName.Text;

            entities.AddToEmployee(emp);

            entities.SaveChanges();

            RefreshGrid();

        }

        

        //update a name of the employee in the table on the basis

        // of id entered in the textbox

        protected void btnUpdate_Click(object sender, EventArgs e)

        {

            CloudEntities entities = new CloudEntities();

            int empId = Convert.ToInt32(txtId.Text);

            Employee employee = entities.Employee.Where(emp => emp.Id ==
                                                               empId).FirstOrDefault();

            if (employee != null)

                employee.Name = txtName.Text;

            entities.SaveChanges();

            RefreshGrid();

        }

        

        //delete a record from the table on the basis of id

        //entered in the textbox

        protected void btnDelete_Click(object sender, EventArgs e)

        {

            CloudEntities entities = new CloudEntities();

            int empId = Convert.ToInt32(txtId.Text);

            Employee employee = entities.Employee.Where(emp => emp.Id ==
                                                               empId).FirstOrDefault();

            if (employee != null)

                entities.DeleteObject(employee);

            entities.SaveChanges();

            RefreshGrid();

        }

 

Now press F5 to run an application. The output window should something like below. Enter an Id and Name and click on Insert button. It will insert a record in the database and display all the records in the gridview.

Insert, Update, Delete in Windows Azure Application

Now enter the Id of an existing employee and update his or her name in the textbox and click on Update. It will update a record if the id is exists in the database and display in gridview.

Insert, Update, Delete in Windows Azure Application

Now again enter the id of an existing employee in the textbox and click on delete button in order the delete the record from the database of an existing employee and from the gridview.

Insert, Update, Delete in Windows Azure Application

I think after reading this article you can easily perform insert, update, delete and select command in any windows azure application by using ADO.NET Entity Model.

If you want to use SQL Azure management portal to perform insert, update, delete and select, change connection string in the web.config and provide connection string of SQL Azure.


Thank you

By anil babu on   4 years ago
I can create Entity data model in the same way it is created but I am writing code In separate class name like "BLCommon" 
I am creating object for datamadel
 "WCFMYModel.WCFMYEntities3 obj = new WCFMYModel.WCFMYEntities3();"
In the above datamodel object "WCFMYModel" in this one not displaying
plz help me

More Details.

By Rohit Kesharwani on   4 years ago
Hi,

Could you please explain your question in more details. I can't understand what exactly you want to do?

Thanks.

Don't want to miss updates? Please click the below button!

Follow MindStick