Perform an INSERT (or ADD), UPDATE and DELETE operations using WCF REST Services with WCF REST Kit

by Jagadish Pulakhandam on 3/2/2012 12:31:21 PM
Rated 0 from 0 votes
Brief: Demonstrates inserting (or adding), updating, deleting rows through a WCF REST service using WCF REST Kit and a simple Windows Forms Client Application. Uses JSON for communication/data exchange.
Posted to: Consuming WCF REST Services
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

The sample demonstrates the following:
  • How to insert, update and delete data using WCF REST service (and WCF REST KIT) using Windows Forms (using HTTP GET operations) with JSON communication
  • How to use HttpClient class available in WCF REST Kit (using HTTP methods GET, POST, PUT and DELETE)
  • How to work with the HttpResponseMessage object (which is a result from HttpClient.Get operation)
  • How to convert .NET custom object to JSON (serializing to JSON)
  • How to convert or deserialize JSON returned from a WCF REST service to a .NET custom object.
NOTE: The samples use JSON to communicate to WCF REST service.  If you need source code of WCF REST Service or XML based communication with WCF REST service, check out "Related Posts" section.

1. Insert/Add operation

Screen shot:



Source Code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports Microsoft.Http
05.Imports SampleEmpServiceLib
06. 
07.Public Class Form1
08. 
09.    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
10.        Dim oEmp As New Employee With {
11.            .Empno = txtEmpno.Text,
12.            .Ename = txtEname.Text,
13.            .Sal = txtSal.Text,
14.            .Deptno = txtDeptno.Text
15.        }
16.        Dim oJson As HttpContent = HttpContentExtensions.CreateJsonDataContract(Of Employee)(oEmp, Nothing)
17.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
18.        Dim resp As HttpResponseMessage = oClient.Post("Employees", oJson)
19.        resp.EnsureStatusIsSuccessful()
20.        MessageBox.Show("Successfully added")
21. 
22.    End Sub
23. 
24.End Class

2. Fetch,Update and Delete operations

Screen shot:



Source Code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports Microsoft.Http
05.Imports SampleEmpServiceLib
06. 
07.Public Class Form1
08. 
09.    Dim uri As String = "http://localhost:8080/SampleEmpService/Employees"
10. 
11.    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
12.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
13.        Dim resp As HttpResponseMessage = oClient.Get("Employees/" & txtEmpno.Text)
14.        resp.EnsureStatusIsSuccessful()
15.        Dim oEmp As Employee = System.Runtime.Serialization.Json.JsonContentExtensions.ReadAsJsonDataContract(Of Employee)(resp.Content)
16. 
17.        Me.txtEname.Text = oEmp.Ename
18.        Me.txtSal.Text = oEmp.Sal
19.        Me.txtDeptno.Text = oEmp.Deptno
20.    End Sub
21. 
22.    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
23.        Dim oEmp As New Employee With {
24.            .Empno = txtEmpno.Text,
25.            .Ename = txtEname.Text,
26.            .Sal = txtSal.Text,
27.            .Deptno = txtDeptno.Text
28.        }
29.        Dim oJson As HttpContent = HttpContentExtensions.CreateJsonDataContract(Of Employee)(oEmp, Nothing)
30.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
31.        Dim resp As HttpResponseMessage = oClient.Put("Employees", oJson)
32.        resp.EnsureStatusIsSuccessful()
33.        MessageBox.Show("Successfully Updated")
34.    End Sub
35. 
36.    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
37. 
38.        Dim oClient As New HttpClient(New Uri("http://localhost:8080/SampleEmpService/"))
39.        Dim resp As HttpResponseMessage = oClient.Delete("Employees/" & txtEmpno.Text)
40.        resp.EnsureStatusIsSuccessful()
41.        MessageBox.Show("Successfully Deleted")
42.    End Sub
43. 
44.End Class

Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following