Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

.NET DataTable To JSON Format Conversion Tool for WCFServices

4.71/5 (6 votes)
4 Nov 2013CPOL2 min read 18K   308  
A .NET v4 Library for converting DataTable row(s) to JSON String for WCFServices

Introduction

The attached plugin is a useful plugin put together to help with converting .NET DataTables to JSON formatted strings, that can be passed through WCFServices without compromising the plumbing.
I put it together because I have developed a lot of custom .NET libraries for use with WCFServices and given the JSON footprint and mobile access to data. I just wanted to have a solution I can easily integrate into my existing applications without having to build new ones.

Background

As I mentioned in the introduction, I am mostly a Windows platform developer, and have a deep love for WCFServices. However, with the proliferation of mobile devices and applications, data consumption needs to be treated with great care by developers. When it comes to transmission format, I recently got wind of JSON and how it seems to be a de-facto mobile data transmission format and have put together a small library (download attached) to help those like myself.

Using the Code

.NET Platform: 4.0

In Visual Studio --> Solution Explorer

  1. Right click on your project and select Add Reference
  2. Select Browse tab and navigate to the location of your decompressed file
  3. Select Munhemba.DataTableToJSON.dll

For C#, do the following:

Add [using Munhemba.DataTableToJSON;]

Within your code, declare the library as follows:

C#
public string GetTableRowsJSON()
{
    string jsonData = null;
    using ([yourDataSetName] [yourDataSet_Alias] = new [yourDataSetName]())
    {
        [yourDataSetName]TableAdapters.[YourTableNameAdapter] 
        [yourTableAdapterAlias] = new [yourDataSetName]TableAdapters.[YourTableNameAdapter]();
        [yourTableAdapterAlias].Fill([yourDataSetName].[YourTableName]);
        Munhemba.DataTableToJSON dtToJSON = 
        	new Munhemba.DataTableToJSON([yourDataSetName].[YourTableName]);
        jsonData = dtToJSON.outputString;
        return jsonData;
    }
}

You may expose the GetTableToRows function through a WCFService the normal way and still get to benefit from the plumbing that comes along with WCF, such as WSHttpBinding, etc.

Happy coding.

Points of Interest

I believe that applications and databases can no longer in fact, should not exist in isolation. WCF provides a tested platform for CRUD on a variety of databases, personally having worked with MSSQL Server, MySQL, ORACLE, and having integrated some of them, I find the JSON format becoming a versatile companion to WCF because of the lightness (message size) of the data transmitted, then again the good relationship it has with JQuery makes it all worth the while to look into and consider.

History

  • First article posted 03 November 2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)