This article contains the following which I used:
  • Used WCF Service
  • Configuration Setting for WCF in web.config
  • Used JavaScript,JQuery and JSON.
  • Insert,Update and Delete data without refreshing page.
SQL Script
  • This is a SQL script for above source code.
  1. CREATE TABLE [dbo].[ProductInfo](
  2. [Id] [int] IDENTITY(1,1) NOT NULL,
  3. [ProductName] [varchar](max) NULL,
  4. [ProductDescription] [varchar](max) NULL,
  5. [CreatedDate] [datetime] DEFAULT GETDATE()
  6. )
WCF Service setting in web.config file
  • This is service configuration setting.
  • We need to add service end point and behavior setting in web.config file.
  • there are so many way to host wcf service like self hosting, WAS, IIS and Window service.
  1. <system.serviceModel>
  2. <behaviors>
  3. <endpointBehaviors>
  4. <behavior name="AspNetAjaxBehavior">
  5. <enableWebScript />
  6. </behavior>
  7. </endpointBehaviors>
  8. </behaviors>
  9. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  10. <services>
  11. <service name="WCFService.ProductService">
  12. <endpoint behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding"
  13. contract="WCFService.IProductService" />
  14. </service>
  15. </services>
  16. </system.serviceModel>
Add Script Reference in Default.aspx page
  • I add a script reference inside scriptmanager.
  1. <asp:ScriptManager runat="server">
  2. <Scripts>
  3. <asp:ScriptReference Path="~/Script/Product.js" />
  4. </Scripts>
  5. <Services>
  6. asp:ServiceReference Path="~/ProductService.svc" />
  7. </Services>
  8. </asp:ScriptManager>
IproductService.cs page
  • The following interface method contain insert, update, delete definition only which is used in service call.
    1. [ServiceContract(Namespace = "WCFService")]
    2. interface IProductService
    3. {
    4. [OperationContract]
    5. void AddProductDetail(string productName, string productDescription);
    6. [OperationContract]
    7. void UpdateProductDetail(int updateId, string productName, string productDescription);
    8. [OperationContract]
    9. void DeleteProductDetail(int id);
    10. [OperationContract]
    11. string EditProductDetail(int id);
    12. [OperationContract]
    13. string LoadAllProductDetail();
    14. }
productService.cs page
  • This file contain functionality which is mentioned in the above interface file.
    1. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    2. public class ProductService : IProductService
    3. {
    4. #region IproductService Member
    5. readonly TestProductEntities _testProductEntities = new TestProductEntities();
    6. [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    7. public string LoadAllProductDetail()
    8. {
    9. try
    10. {
    11. var productDetail = (from s in _testProductEntities.ProductInfoes
    12. select s).ToList();
    13. var serializer = new JavaScriptSerializer();
    14. return (serializer.Serialize(productDetail.ToList()));
    15. }
    16. catch (Exception ex)
    17. {
    18. throw ex;
    19. }
    20. }
    21. #endregion
    22. }
product.js page
  • This is my javascript file and highlighted area displays my service call.
  1. $(document).ready(function () {
  2. LoadProduct();
  3. });
  4. function BindProduct(results) {
  5. var myJsonString = JSON.parse(results);
  6. var string = "<table class='table table-bordered'><tbody>";
  7. string += '<tr><th>Name</th><th>Description</th><th colspan="2">Action</th></tr>';
  8. if (myJsonString.length > 0) {
  9. for (var i = 0; i < myJsonString.length; i++) {
  10. string += '<tr><td>' + myJsonString[i].ProductName + '</td>';
  11. string += '<td>' + myJsonString[i].ProductDescription + '</td>';
  12. string += '<td><span><a href="javascript:void(0);" ' + ' onclick="EditProduct(' + myJsonString[i].Id + ');">';
  13. string += '<img width="16" height="16" alt="Close" src="Image/Edit.jpg" /></a></span></td>';
  14. string += '<td><span><a href="javascript:void(0);" ' + ' onclick="DeleteProduct(' + myJsonString[i].Id + ');">';
  15. string += '<img width="16" height="16" alt="Close" src="Image/close.png" /></a></span></td></tr>';
  16. }
  17. }
  18. string = string + "</tbody></table>";
  19. $("#bindProduct").html(string);
  20. }
  21. function LoadProduct() {
  22. WCFService.IProductService.LoadAllProductDetail(BindProduct, LoadProduct_success, LoadProduct_fail);
  23. }
  24. function LoadProduct_success() {
  25. }
  26. function LoadProduct_fail() {
  27. alert("LoadProduct_fail");
  28. }
Output

output