Perform an INSERT (or ADD) operation using WCF REST Services (with JSON Communication)

by Jagadish Pulakhandam on 2/22/2012 9:55:46 AM
Rated 0 from 0 votes
Brief: Demonstrates inserting/adding a new record through a WCF REST service using 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/add data to database using WCF REST service using Windows Forms (using HTTP POST operation) with JSON communication
  • How to use WebRequest class to post to WCF REST service
  • How to use HttpWebRequest and HttpWebResponse to work with WCF REST service
  • Working 'Stream' object returned by HttpWebResponse
  • How to convert or serialize a .NET object to JSON in order to work with WCF REST service
  • How to use DataContractJsonSerializer (available in System.Runtime.Serialization.Json namespace)
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.


Source code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports System.Runtime.Serialization.Json
06.Public Class Form1
08.    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
09.        Dim uri As String = "http://localhost:8080/SampleEmpService/Employees"
11.        Dim oEmp As New Employee With {
12.            .Empno = txtEmpno.Text,
13.            .Ename = txtEname.Text,
14.            .Sal = txtSal.Text,
15.            .Deptno = txtDeptno.Text
16.            }
18.        Dim ser As New DataContractJsonSerializer(GetType(Employee))
19.        Dim ms As New MemoryStream
20.        ser.WriteObject(ms, oEmp)
22.        'Dim jsonData As String = Encoding.UTF8.GetString(ms.ToArray)
23.        'MessageBox.Show(jsonData)
26.        Dim arr() As Byte = ms.ToArray()
27.        Dim req As HttpWebRequest = WebRequest.Create(uri)
28.        req.Method = "POST"
29.        req.ContentType = "application/json"
30.        req.ContentLength = arr.Length
32.        Dim reqStrm As Stream = req.GetRequestStream()
33.        reqStrm.Write(arr, 0, arr.Length)
34.        reqStrm.Close()
36.        Dim resp As HttpWebResponse = req.GetResponse()
37.        MessageBox.Show(resp.StatusDescription)
39.        ''only if necessary
40.        'Dim respStream As Stream = resp.GetResponseStream
41.        'Dim sr As New StreamReader(respStream)
42.        'MessageBox.Show(sr.ReadToEnd)
43.        'sr.Close()
45.        reqStrm.Close()
46.        resp.Close()
48.    End Sub
50.    Public Class Employee
51.        Public Empno As String
52.        Public Ename As String
53.        Public Sal As String
54.        Public Deptno As String
55.    End Class
57.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

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