Create an ASP.NET application.

Add a folder named Service to the project.

Add a WCF Service in the Service folder as in the following:

wcf service

You will see that two files are create, one has a .svc extension and the other is the interface class.

Open Iservice1.cs interface.

Add a method to get the Customer Name as in the following.

If Webinvoke is not found then add the reference from .Net as in the following:

  1. namespace AutoComplete_WCF_Jquery_AJAX_Call.Service {
  2. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
  3. [ServiceContract]
  4. public interface IService1 {
  5. [OperationContract]
  6. [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
  7. string GetCustomers(string AutoData);
  8. }
  9. }

Add Method

Add a method under the .Svc class like below. We must define a connection string in webconfig or you can define one in this class also:

  1. namespace TestAppwith_Jquery.Services {
  2. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
  3. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  4. public class Service: IService {
  5. public string GetCustomers(string prefix) {
  6. List < object > customers = new List < object > ();
  7. using(SqlConnection conn = new SqlConnection()) {
  8. conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
  9. using(SqlCommand cmd = new SqlCommand()) {
  10. cmd.CommandText = "SELECT * FROM dbo.studentmaster where " +
  11. "StdName like @prefix + '%'";
  12. cmd.Parameters.AddWithValue("@prefix", prefix);
  13. cmd.Connection = conn;
  14. conn.Open();
  15. using(SqlDataReader sdr = cmd.ExecuteReader()) {
  16. while (sdr.Read()) {
  17. customers.Add(new {
  18. Id = sdr["AdmNo"],
  19. Name = sdr["StdName"]
  20. });
  21. }
  22. }
  23. conn.Close();
  24. }
  25. return (new JavaScriptSerializer().Serialize(customers));
  26. }
  27. }
  28. }
  29. }

To call WCF Service in aspx page add the following code:

  1. <html
  2. xmlns="http://www.w3.org/1999/xhtml">
  3. <head id="Head1" runat="server">
  4. <title></title>
  5. <style>
  6. table, th , td {
  7. border: 1px solid grey;
  8. border-collapse: collapse;
  9. padding: 5px;
  10. }
  11. table tr:nth-child(odd) {
  12. background-color: #f1f1f1;
  13. }
  14. table tr:nth-child(even) {
  15. background-color: #ffffff;
  16. }
  17. </style>
  18. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  19. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  20. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  21. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
  22. <script type = "text/javascript">
  23. $(document).ready(function () {
  24. $("#search12").click(function () {
  25. $.ajax({
  26. type: "POST",
  27. contentType: "application/json; charset=utf-8",
  28. url: "../Service/Service1.svc/GetCustomers",
  29. data: '{"AutoData": "' + $("#AutoData").val() + '"}',
  30. processData: false,
  31. dataType: "json",
  32. success: function (response) {
  33. var customers = eval(response.d);
  34. var html = "";
  35. html += "
  36. <table >
  37. <tr>
  38. <td>ID</td>
  39. <td>Name</td>
  40. </tr>";
  41. $.each(customers, function () {
  42. html += "
  43. <tr>
  44. <td>" + this.Id + "</td>
  45. <td>" + this.Name + "</td>
  46. </tr>";
  47. });
  48. html += "
  49. </table>";
  50. $("#results").html(html == "" ? "No results" : html);
  51. },
  52. error: function (a, b, c) {
  53. alert(a.responseText);
  54. }
  55. });
  56. });
  57. });
  58. </script>
  59. </head>
  60. <body>
  61. <form id="form1" runat="server">
  62. <input type = "text" id = "AutoData" />
  63. <input id = "search12" type = "button" value = "Search" />
  64. <div id = "results"></div>
  65. </form>
  66. </body>
  67. </html>

Change in the web config as in the following code to add your Service Name and contract Name:

  1. <system.serviceModel>
  2. <behaviors>
  3. <serviceBehaviors>
  4. <behavior name="ServiceBehavior">
  5. <serviceMetadata httpGetEnabled="true"/>
  6. <serviceDebug includeExceptionDetailInFaults="true"/>
  7. </behavior>
  8. </serviceBehaviors>
  9. <endpointBehaviors>
  10. <behavior name="ServiceAspNetAjaxBehavior">
  11. <enableWebScript/>
  12. </behavior>
  13. </endpointBehaviors>
  14. </behaviors>
  15. <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  16. <services>
  17. <service behaviorConfiguration="ServiceBehavior" name="AutoComplete_WCF_Jquery_AJAX_Call.Service.Service1">
  18. <endpoint address="" binding="webHttpBinding" contract="AutoComplete_WCF_Jquery_AJAX_Call.Service.IService1" behaviorConfiguration="ServiceAspNetAjaxBehavior">
  19. <identity>
  20. <dns value="localhost"/>
  21. </identity>
  22. </endpoint>
  23. <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  24. </service>
  25. </services>
  26. </system.serviceModel>

The output will be like the following:

list