Monday, August 5, 2013

Get Employees list using Jquery Ajax,WCF Restful Service and Entity Framework

Get Employees list using Jquery Ajax,WCF Restful Service
In this article i would like to explain "How to get employees list using Jquery Ajax,Wcf Restful service and Entity Framework".This is our first article in this series
Data Access Layer : 

First, Open the Visual studio and select a new project Next, Change the Name as SeriesEntity.(see the below pictures) 













Next, Delete the default class (class1.cs) 

Next, Right click on the project and select Add new item 

Next, Select ADO.NET Entity data model.

Next, Change the name as EmployeeModel.edmx

After successfully adding the project. Business Layer 

RightClick on the solution and select new class library 

Next, change the name as SeriesBO

Next, Change the class name as EMP 

 Next, Add the SeriesEntity dll reference to this SeriesBO project(for this you need to rightclick on the project and select Add reference Tab and then select Seriesentity dll file) and write the following code inside the class 

---------------------------------------------------------------------------------------------

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using SeriesEntity; 

 namespace SeriesBO 
{
      public class Emp 
      { 
         public List GetAllEmployee() 
          { 
            dbInvent db = new dbInvent();
            List boemp = new List(); 
            return boemp = db.Employees.ToList(); 
          }
     }
 } 
---------------------------------------------------------------------------------------------
 Now, Build the project of seriesBO 

Service Integration Layer

Right click on the solution and select new WCF service application project. Next, Change the project name as SeriesService

 Next, Add SeriesBO dll to SeriesService project 

 Next, Open te Service1.svc file and write the following code 

---------------------------------------------------------------------------------------------

using System; 
using System.Collections.Generic;
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text;
using System.ServiceModel.Activation; 
using SeriesBO;
using SeriesEntity; 
using Newtonsoft.Json; 

namespace SeriesService 

 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

 public class Service1 : IService1 
 { 
      Emp emps = new Emp(); 
      public string GetEmployees() 
       { 
          IList list = emps.GetAllEmployee();

        string json = JsonConvert.SerializeObject(list.Select(m => new {                          CustomerID = m.EmployeeID, FirstName = m.FirstName, LastName                = m.LastName, Status = m.Status } )); 
              return json;
            } 
       } 


---------------------------------------------------------------------------------------------

Note : Here we have added Newtonsoft.json library file to our project.
you can download this file source code 

Now,Open the Iservice.cs file and write the following code 

---------------------------------------------------------------------------------------------

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace SeriesService 
{ // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 

 [ServiceContract] public interface IService1 { [OperationContract] 
 [WebGet(UriTemplate = "GetEmployees", BodyStyle = WebMessageBodyStyle.Bare)] 

     string GetEmployees(); 
   } 
 } 

---------------------------------------------------------------------------------------------

 Most Important part : 

 Open the web config file and replace the following code in between the system.servicemodel 

 Now build the project. That's it we have successfully created the service. 

you can check the output in browser by using the following url http://localhost/Service1.svc/GetEmployees 

Client Application : 

 Now, its time to create a client application 
Right click on the solution and select new asp.net website or web application as per your requirement. 

Next, Open the Default.aspx page and add one gridview 

Next, Open the default.aspx.cs page and Add the following namespaces 

---------------------------------------------------------------------------------------------

using System;
using System.Net; 
using System.IO; 
using System.Web.Script.Serialization; 
using System.Data; using Newtonsoft.Json; 

Next,write the following code in page load 

string URL = "http://localhost:4850/Service1.svc/GetEmployees"; 
string ResponseType = "Json"; 
WebClient client = new WebClient(); 
Stream data = client.OpenRead(URL); 
client.Headers["Content-type"] = @"application/" + ResponseType; 
StreamReader reader = new StreamReader(data); 
string str = string.Empty; str = reader.ReadLine(); 
JavaScriptSerializer ser = new JavaScriptSerializer(); 
DataTable dt = JsonConvert.DeserializeObject(ser.DeserializeObject(str).ToString()); GridView1.DataSource = dt; GridView1.DataBind(); 

---------------------------------------------------------------------------------------------

Now build the project and run the default.aspx page. That's it..

Labels: , , , , , , , , ,