Binding a list of resources from a WCF REST Services to a DataGridView in Windows Form (using JSON serialization/deserialization)

by Jagadish Pulakhandam on 2/10/2012 8:58:47 AM
Rated 0 from 0 votes
Brief: Demonstrates consuming a WCF REST service and binding the list of resources to DataGridView control in 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 fetch data from WCF REST service using Windows Forms (using HTTP GET operations) with JSON communication
  • How to use WebRequest class to fetch from 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 deserialize JSON returned from a WCF REST service to a .NET list object.
  • How to bind JSON returned from WCF REST service to DataGridVew control in Windows Forms
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.

Screen shot (form design):



Source Code (in VB.NET):

01.Imports System.Net
02.Imports System.IO
03.Imports System.Text
04.Imports System.Runtime.Serialization.Json
05. 
06.Public Class Form1
07. 
08.    Private Sub btnShowEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowEmployees.Click
09.        Dim uri As String = "http://localhost:8080/SampleEmpService/Employees"
10.        Dim req As HttpWebRequest = WebRequest.Create(uri)
11.        Dim resp As HttpWebResponse = req.GetResponse()
12.        Dim strm As Stream = resp.GetResponseStream()
13. 
14.        'Dim sr As New StreamReader(strm, Encoding.UTF8)
15.        'MessageBox.Show(sr.ReadToEnd)
16. 
17.        Dim ser As New DataContractJsonSerializer(GetType(List(Of Employee)))
18.        Dim lstEmp As List(Of Employee) = CType(ser.ReadObject(strm), List(Of Employee))
19. 
20.        Me.DataGridView1.DataSource = lstEmp
21. 
22.        strm.Close()
23.        resp.Close()
24. 
25.    End Sub
26. 
27.    Public Class Employee
28.        Public Property Empno As String
29.        Public Property Ename As String
30.        Public Property Sal As String
31.        Public Property Deptno As String
32.    End Class
33. 
34.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