Update Data Using OData Service Via EDM Framework

Introduction

 

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

Question: What is updating data using OData Service via EDM Framework?

In simple terms "It provides flexibility to update the data with the help of data services using the EDM framework".

Step 1: Create a new web application

 

Output1.jpg
 

Step 2: Add a new ADO.NET entity data model, as in:

Output2.jpg
 

Output3.png
 

Output4.png
 

Output5.png
 

Output6.png

Step 3: Add the service reference of the OData Service, as in:

Output7.png
 

Step 4: Create a new web application, as in:

 

Output8.png
 

Step 5: Browse the service, as in:

 

Output9.png
 

Step 6: The complete code of ODataService.svc.cs

 

using System;

using System.Collections.Generic;

using System.Data.Services;

using System.Data.Services.Common;

using System.Linq;

using System.ServiceModel.Web;

using System.Web;namespace UpdateODataApp

{

    public class ODataService : DataService< CompanyEntities >

{

        // This method is called only once to initialize service-wide policies.public static void InitializeService(DataServiceConfiguration config)

    {

        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

        // Examples:config.SetEntitySetAccessRule("*", EntitySetRights.All);

        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

    }

}

}

 

Step 7: Add the service reference to the new web application, as in:

 

Output10.png
 

Step 8: The complete code of WebForm1.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

 

<!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="Update Data with OData Service via EDM Framework"

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

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label6" runat="server" Text="Please Enter Employee Id" Font-Size="Large"

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

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

                        <asp:Button ID="Button2" runat="server" Text="Update Data" Font-Names="Verdana" Width="166px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button2_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2" 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 9: The complete code of WebForm1.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 WebApplication1.ServiceReference1;namespace WebApplication1

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void Button2_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 = "Please Enter Some Values";

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

            }

            else

            {

                var updateQuery = (from r in objEntities.Employee where r.EmpId == int.Parse(TextBox4.Text) select r).Single();

                updateQuery.FirstName = TextBox1.Text;

                updateQuery.LastName = TextBox2.Text;

                updateQuery.Age = int.Parse(TextBox3.Text);

                updateQuery.Location = TextBox5.Text;

                objEntities.UpdateObject(updateQuery);

                objEntities.SaveChanges();

                Label5.Text = "Data Updated Successfully";

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

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

                TextBox3.Text = string.Empty;

                TextBox4.Text = string.Empty;

                TextBox5.Text = string.Empty;

            }

        }

        #region Instance MembersCompanyEntities objEntities = new CompanyEntities(new Uri("http://localhost:5430/ODataService.svc/"));

        #endregion

    }

}

 

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

Output11.png
 

Step 11: The updated data output of the application looks like this:
 

Output12.png
 

I hope this article is useful for you.

 


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