Click here to Skip to main content
15,884,298 members
Articles / Web Development / ASP.NET
Tip/Trick

.NET DataTable To JSON Format Conversion Tool for WCFServices

Rate me:
Please Sign up or sign in to vote.
4.71/5 (6 votes)
4 Nov 2013CPOL2 min read 17.7K   308   8   2
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)


Written By
Software Developer
South Africa South Africa
My IT Career began in 1996 as I was studying to become a Chartered Accountant.

Between 1998 to Date I have worked as a Computer Technician, IT Manager, Tutor and to my present position of Developer in a company I co-own with a colleague based in Johannesburg South Africa.

We do Web Hosting, design and development, and have only recently added Dot.Net Enterprise Application development to the business portfolio of Services.

Comments and Discussions

 
QuestionQuestion Pin
Member 104478645-Dec-13 10:28
Member 104478645-Dec-13 10:28 
AnswerRe: Question Pin
SimbarasheM5-Dec-13 23:34
SimbarasheM5-Dec-13 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.