ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.WCF JSON Rest Service Select Insert Update Delete (CRUD) in C# and VB.Net<p>Hi&nbsp;nauna,</p> <p>Refering the below article i have created an example.</p> <h1 class="header"><a title="Simple WCF JSON Rest Service Tutorial with example in C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Simple-WCF-JSON-Rest-Service-Tutorial-with-example-in-C-and-VBNet.aspx">Simple WCF JSON Rest Service Tutorial with example in C# and VB.Net</a></h1> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head id=&#34;Head1&#34; runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script src=&#34;Scripts/jquery-1.4.1.min.js&#34; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;#btnSearch&#34;).live(&#34;click&#34;, function () { $.ajax({ type: &#34;POST&#34;, contentType: &#34;application/json; charset=utf-8&#34;, url: &#39;&lt;%=ResolveUrl(&#34;~/Services/Service.svc/GetCustomers&#34;) %&gt;&#39;, data: &#39;{&#34;prefix&#34;: &#34;&#39; + $(&#34;#prefix&#34;).val() + &#39;&#34;}&#39;, processData: false, dataType: &#34;json&#34;, success: function (response) { var customers = eval(response.d); var html = &#34;&lt;table&gt;&lt;tr&gt;&#34;; html += &#34;&lt;td&gt;Id&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Country&lt;/td&gt;&lt;/tr&gt;&#34;; $.each(customers, function () { html += &#34;&lt;tr&gt;&lt;td&gt;&#34; + this.Id + &#34;&lt;/td&gt;&lt;td&gt;&#34; + this.Name + &#34;&lt;/td&gt;&lt;td&gt;&#34; + this.Country + &#34;&lt;/td&gt;&lt;/tr&gt;&#34;; }); html += &#34;&lt;/table&gt;&#34;; $(&#34;#results&#34;).html(html == &#34;&#34; ? &#34;No results&#34; : html); }, error: function (a, b, c) { alert(a.responseText); } }); }); $(&#34;#btnInsert&#34;).live(&#34;click&#34;, function () { $.ajax({ type: &#34;POST&#34;, contentType: &#34;application/json; charset=utf-8&#34;, url: &#39;&lt;%=ResolveUrl(&#34;~/Services/Service.svc/InsertCustomer&#34;) %&gt;&#39;, data: &#39;{&#34;name&#34;: &#34;&#39; + $(&#34;#txtName&#34;).val() + &#39;&#34;,&#34;country&#34;: &#34;&#39; + $(&#34;#txtCountry&#34;).val() + &#39;&#34;}&#39;, processData: false, dataType: &#34;json&#34;, success: function (response) { alert(&#39;Record Inserted successfully&#39;); }, error: function (a, b, c) { alert(a.responseText); } }); }); $(&#34;#btnUpdate&#34;).live(&#34;click&#34;, function () { $.ajax({ type: &#34;POST&#34;, contentType: &#34;application/json; charset=utf-8&#34;, url: &#39;&lt;%=ResolveUrl(&#34;~/Services/Service.svc/UpdateCustomer&#34;) %&gt;&#39;, data: &#39;{&#34;id&#34;: &#39; + $(&#34;#txtId&#34;).val() + &#39;,&#34;name&#34;: &#34;&#39; + $(&#34;#txtName&#34;).val() + &#39;&#34;,&#34;country&#34;: &#34;&#39; + $(&#34;#txtCountry&#34;).val() + &#39;&#34;}&#39;, processData: false, dataType: &#34;json&#34;, success: function (response) { alert(&#39;Record updated successfully&#39;); }, error: function (a, b, c) { alert(a.responseText); } }); }); $(&#34;#btnDelete&#34;).live(&#34;click&#34;, function () { $.ajax({ type: &#34;POST&#34;, contentType: &#34;application/json; charset=utf-8&#34;, url: &#39;&lt;%=ResolveUrl(&#34;~/Services/Service.svc/DeleteCustomer&#34;) %&gt;&#39;, data: &#39;{&#34;id&#34;: &#39; + $(&#34;#txtId&#34;).val() + &#39;}&#39;, processData: false, dataType: &#34;json&#34;, success: function (response) { alert(&#39;Record deleted successfully&#39;); }, error: function (a, b, c) { alert(a.responseText); } }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;input type=&#34;text&#34; id=&#34;prefix&#34; /&gt; &lt;input id=&#34;btnSearch&#34; type=&#34;button&#34; value=&#34;Search&#34; /&gt; &lt;div id=&#34;results&#34;&gt; &lt;/div&gt; &lt;br /&gt; &lt;table border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;td&gt; Id &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtId&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Name &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Country &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtCountry&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan=&#34;2&#34;&gt; &lt;asp:Button ID=&#34;btnInsert&#34; Text=&#34;Insert&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnUpdate&#34; Text=&#34;Update&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnDelete&#34; Text=&#34;Delete&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">IService.cs</span></strong></p> <pre class="brush: csharp">using System.ServiceModel; using System.ServiceModel.Web; [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = &#34;POST&#34;, ResponseFormat = WebMessageFormat.Json)] string GetCustomers(string prefix); [OperationContract] [WebInvoke(Method = &#34;POST&#34;, ResponseFormat = WebMessageFormat.Json)] void InsertCustomer(string name, string country); [OperationContract] [WebInvoke(Method = &#34;POST&#34;, ResponseFormat = WebMessageFormat.Json)] void UpdateCustomer(int id, string name, string country); [OperationContract] [WebInvoke(Method = &#34;POST&#34;, ResponseFormat = WebMessageFormat.Json)] void DeleteCustomer(int id); } </pre> <p><strong><u>Service.cs</u></strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.ServiceModel.Activation; using System.Web.Script.Serialization; [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service : IService { public string GetCustomers(string prefix) { List&lt;object&gt; customers = new List&lt;object&gt;(); using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT * FROM Customers WHERE Name = @prefix OR @prefix IS NULL&#34;; cmd.Parameters.AddWithValue(&#34;@prefix&#34;, !string.IsNullOrEmpty(prefix) ? prefix : (object)DBNull.Value); cmd.Connection = conn; conn.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { customers.Add(new { Id = sdr[&#34;CustomerId&#34;], Name = sdr[&#34;Name&#34;], Country = sdr[&#34;Country&#34;] }); } } conn.Close(); } return (new JavaScriptSerializer().Serialize(customers)); } } public void InsertCustomer(string name, string country) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;INSERT INTO Customers VALUES(@Name,@Country)&#34;; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name); cmd.Parameters.AddWithValue(&#34;@Country&#34;, country); cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } public void UpdateCustomer(int id, string name, string country) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;UPDATE Customers SET Name=@Name,Country=@Country WHERE CustomerId=@Id&#34;; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name); cmd.Parameters.AddWithValue(&#34;@Country&#34;, country); cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } public void DeleteCustomer(int id) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;DELETE FROM Customers WHERE CustomerId=@Id&#34;; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); cmd.Connection = conn; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } }</pre> <p><strong><span style="text-decoration: underline;">Service.vb</span></strong></p> <pre class="brush: vb">Imports System.Collections.Generic Imports System.Runtime.Serialization Imports System.ServiceModel Imports System.Data.SqlClient Imports System.Configuration Imports System.Web.Script.Serialization Imports System.ServiceModel.Activation Imports System.ServiceModel.Web Imports System.Web.Script.Services &lt;ServiceContract()&gt; _ &lt;AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)&gt; _ Public Class Service &lt;OperationContract()&gt; _ &lt;WebInvoke(Method:=&#34;POST&#34;, ResponseFormat:=WebMessageFormat.Json)&gt; _ Public Function GetCustomers(ByVal prefix As String) As String Dim customers As List(Of Object) = New List(Of Object)() Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = &#34;SELECT * FROM Customers WHERE Name = @prefix OR @prefix IS NULL&#34; cmd.Parameters.AddWithValue(&#34;@prefix&#34;, If(Not String.IsNullOrEmpty(prefix), prefix, CObj(DBNull.Value))) cmd.Connection = conn conn.Open() Using sdr As SqlDataReader = cmd.ExecuteReader() While sdr.Read() customers.Add(New With { _ Key .Id = sdr(&#34;CustomerId&#34;), _ Key .Name = sdr(&#34;Name&#34;), _ Key .Country = sdr(&#34;Country&#34;) _ }) End While End Using conn.Close() End Using Return (New JavaScriptSerializer().Serialize(customers)) End Using End Function &lt;OperationContract()&gt; _ &lt;WebInvoke(Method:=&#34;POST&#34;, ResponseFormat:=WebMessageFormat.Json)&gt; _ Public Sub InsertCustomer(ByVal name As String, ByVal country As String) Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = &#34;INSERT INTO Customers VALUES(@Name,@Country)&#34; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name) cmd.Parameters.AddWithValue(&#34;@Country&#34;, country) cmd.Connection = conn conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End Sub &lt;OperationContract()&gt; _ &lt;WebInvoke(Method:=&#34;POST&#34;, ResponseFormat:=WebMessageFormat.Json)&gt; _ Public Sub UpdateCustomer(ByVal id As Integer, ByVal name As String, ByVal country As String) Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = &#34;UPDATE Customers SET Name=@Name,Country=@Country WHERE CustomerId=@Id&#34; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name) cmd.Parameters.AddWithValue(&#34;@Country&#34;, country) cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) cmd.Connection = conn conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End Sub &lt;OperationContract()&gt; _ &lt;WebInvoke(Method:=&#34;POST&#34;, ResponseFormat:=WebMessageFormat.Json)&gt; _ Public Sub DeleteCustomer(ByVal id As Integer) Using conn As SqlConnection = New SqlConnection() conn.ConnectionString = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using cmd As SqlCommand = New SqlCommand() cmd.CommandText = &#34;DELETE FROM Customers WHERE CustomerId=@Id&#34; cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) cmd.Connection = conn conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End Sub End Class </pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/iTfpgYI.gif" alt="" width="320" height="424" /></span></strong></p>https://www.aspforums.net:443/Threads/145069/WCF-JSON-Rest-Service-Select-Insert-Update-Delete-CRUD-in-C-and-VBNet/https://www.aspforums.net:443/Threads/145069/WCF-JSON-Rest-Service-Select-Insert-Update-Delete-CRUD-in-C-and-VBNet/Mon, 12 Nov 2018 06:05:00 GMT