jQuery Datatables with C# / .NET web services

I just completed a project where I integrated that jQuery Datatables plugin http://datatables.net. The documentation from the Datatables website was helpful but there were a gaps that needed extended documentation.

This particular implementation uses an ASMX service for the underlying service. The plugin is integrated with my <table> with the following call.

        bJQueryUI: true,
        sPaginationType: "full_numbers",
        bProcessing: true,
        bServerSide: true,
        sAjaxSource: '/Services/MyService.asmx/MyWebMethod',
        fnServerData: function (sSource, aoData, fnCallback) {
            var jsonIn = '{ delimiter: "|", delimitedTags: "' + strTags + '", pageSize: 100, skip: ' + aoData[3].value + ', sortColumn: ' + aoData[47].value + ', sortDirection: "' + aoData[48].value + '", echo: ' + aoData[0].value + ' }';

                contentType: 'application/json',
                dataType: 'json',
                error: ajaxError,
                type: "POST",
                url: sSource,
                data: jsonIn,
                success: function (msg) {

The ajax call is to an ASP.NET Web Service (.asmx) so the data needs to be sent as text.
fnCallback is the rendering function for the datatables plugin. The plugin requires the construct passed in to have the following properties:

  • sEcho
  • iTotalRecords
  • iTotalDisplayRecords
  • sColumns (optional)
  • aaData

Following Zack Owens’s advice, I created a server side class that wraps my web service’s response. The properties correspond to the data contract that this plugin expects. For the sEcho property, needs to be populated with the same value that is passed to the fnServerData handler via aoData[0].value otherwise your data table will experience any number of issues re-rendering when you sort or change pages.

    public class FormatedList
        public FormatedList()
        public int sEcho { get; set; }
        public int iTotalRecords { get; set; }
        public int iTotalDisplayRecords { get; set; }
        public List aaData { get; set; }
        public string sColumns { get; set; }
        public void Import(string[] properties)
            sColumns = string.Empty;
            for (int i = 0; i < properties.Length; i++)
                sColumns += properties[i];
                if (i < properties.Length - 1)
                    sColumns += ",";

Adventures in WCF (Windows Communication Foundation)

My latest project at work has me working with WCF for the first time and it has been an interesting experience. Over the next few weeks, I will be discussing configuration, debugging, and deployment of the WCF service and connecting to it.

Microsoft provides a few tools that can help debug your WCF services.

Error Handling

Typically, I add Global.asax to a web application in ASP.NET and use the Application_Error event to handle any uncaught exceptions.

protected void Application_Error(object sender, EventArgs e)
//Insert your application’s exception logging here

This doesn’t work (still looking into why) for WCF services, even when hosted on IIS. A good work around is to wrap each service method with a try…catch block and calling the exception logger in the catch block.

public Object MyWcfWrapperMethod( int i )
//Call the method being exposed by WCF
catch( Exception e ){
//Log the exception
//do your clean up here


Unit tests are especially helpful (aren’t they always?) in the development of your WCF services as messages from errror and exceptions can (and will) be hidden by subsequent errors. An invalid parameter to a stored procedure call can result in a high level System.ServiceModel.CommunicationObjectFaultedException.