WCFCall.aspx——————————————————
First Register your ajax enabled WCF service
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
<Services>
<asp:ServiceReference Path=”~/Service.svc” />
</Services>
</asp:ScriptManager>
<asp:Button ID=”btnCall” runat=”server” Text=”Get Record” />
<div id=”bindData”>
Add below Section to Head
<script src=”jquery-1.7.2.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function () {
//Button Click Function
$(“input[id$=btnCall]”).click(function () {
//Get Record by Ajax Enabled WCF Service
Service.GetAllRecord(onSuccess, OnFailed);
return false;
});
// Table Creating Funtion
function GetElementDetail(id, name, price) {
var detail = “<tr>”;
detail += “<td>”;
detail += “” + id + “</td>”;
detail += “<td>”;
detail += ” ” + name + “</td>”;
detail += “<td>”;
detail += “” + price + “</td>”;
detail += “<td>”;
detail += “” + “<a href=’javascript:Edit(” + id + “)’ title=’Edit Item’ >Edit</a><a href=’javascript:Delete(” + id + “)’ title=’Delete Item’ onclick=’javascript:if(!confirm(\”Are you sure you want to delete ?\”)) return false’ >Delete</a></td>”;
detail += “</tr>”;
return detail;
}
//Success Function
function onSuccess(result) {
var tableString = “”;
$(“#bindData”).html(“”);
var tableHead = “<thead style=’background-color: orange;’><tr><th>ID </th><th>Name</th><th>Price</th><th>Actions</th></tr></thead>”;
$(result).each(function (i) {
var id = result[i].id;
var name = result[i].name;
var price = result[i].price;
var strInner = GetElementDetail(id, name, price);
tableString = tableString + strInner;
});
$(“#bindData”).append(tableHead + tableString);
return false;
}
//Failed Function
function OnFailed() {
alert(‘Failed to Load Data.’);
}
});
function Edit(obj) {
alert(“Perform Edit Operation by ID ” + obj + “”)
}
function Delete(obj) {
alert(“Perform Delete Operation by ID” + obj + “”);
}
</script>
Use Ajax Enabled WCF Service
Service.cs————————————————————–
[OperationContract]
public GetDetails[] GetAllRecord()
{
// Add your operation implementation here
DataTable dt = GetProductDataTable();
List<GetDetails> objGet = new List<GetDetails>();
for (int i = 0; i < dt.Rows.Count; i++)
{
objGet.Add(new GetDetails() {
id=Convert.ToInt32(dt.Rows[i][“ID”]),
name=dt.Rows[i][“NAME”].ToString(),
price=Convert.ToInt32(dt.Rows[i][“PRICE”])
});
}
return objGet.ToArray();
}
public DataTable GetProductDataTable()
{
// Here we create a DataTable with three columns.
DataTable dtproduct = new DataTable();
dtproduct.Columns.Add(“ID”, typeof(int));
dtproduct.Columns.Add(“NAME”, typeof(string));
dtproduct.Columns.Add(“PRICE”, typeof(int));
// Here we add five DataRows.
dtproduct.Rows.Add(1, “Product1”, 100);
dtproduct.Rows.Add(2, “Product2”, 110);
dtproduct.Rows.Add(3, “Product3”, 120);
dtproduct.Rows.Add(4, “Product4”, 130);
dtproduct.Rows.Add(5, “Product5”, 140);
return dtproduct;
}
public struct GetDetails
{
public int id { get; set; }
public string name { get; set; }
public int price { get; set; }
}
Download Code
Get Records by WCF