Q:

How to return datatable form wcf rest services?

I want to return datatable  form wcf rest service to  c# form,but following error raised when returning datatable form server to client.

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

A:

I have got Solution to this problem.
When we return datatable form wcf rest Services then serialization problem occur with datatable.
           to avoid this problem convert datatable in to json stirg,and return this string to client side.
               on the client side convert this string to datatable.

Server Side Code:
       public string GetAssetRegisterDataTableBySp(ParameterDto parameters)
    {
        string jsonString="";
        string query = string.Format("SELECT * FROM ReportDisplaySetting where Display= 'true'");
        var DisplayColumnSetting = repositoryReportDisplaySetting.ExecuteRawQuery<ReportDisplaySetting>(query).ToList();
        List<string> selectedColumn = DisplayColumnSetting.Select(C => C.ColumnName).ToList();
        string columnOfString = (string.Join(",", selectedColumn.Select(x => x.ToString()).ToArray()));

        List<string> locationIds = new List<string>();
        if (parameters.locationId == 0)
        {
            locationIds = repository.All<CompanyLocationMaster>().AsEnumerable().Where(x => x.CompanyID == parameters.companyId && x.IsActive == true).Select(x => x.LocationID.ToString()).ToList();
        }
        string locationString = "";
        if (locationIds.Count() > 0)
        {
            locationString = string.Join(",", locationIds.ToArray());
        }
        else
        {
            locationString = parameters.locationId.ToString();
        }
        var report = new AssetrakRepository<AssetRegisterReport>();
        DataTable taggingReport = report.GetAssetRegisterReportBySP(parameters.companyId, locationString, columnOfString);
        string strColumns = columnOfString;
        var JSONString = new StringBuilder();
        if (taggingReport.Rows.Count > 0)
        {
            JSONString.Append("[");
            for (int i = 0; i < taggingReport.Rows.Count; i++)
            {
                JSONString.Append("{");
                for (int j = 0; j < taggingReport.Columns.Count; j++)
                {
                    if (j < taggingReport.Columns.Count - 1)
                    {
                        JSONString.Append("\"" + taggingReport.Columns[j].ColumnName.ToString() + "\":" + "\"" + taggingReport.Rows[i][j].ToString().Replace(","," ") + "\",");
                    }
                    else if (j == taggingReport.Columns.Count - 1)
                    {
                        JSONString.Append("\"" + taggingReport.Columns[j].ColumnName.ToString() + "\":" + "\"" + taggingReport.Rows[i][j].ToString().Replace(",", " ") + "\"");
                    }
                }
                if (i == taggingReport.Rows.Count - 1)
                {
                    JSONString.Append("}");
                }
                else
                {
                    JSONString.Append("},");
                }
            }
            JSONString.Append("]");

        }
        jsonString=JSONString.ToString();

        return jsonString;
    }

Client Side Code:
        DataTable dt = ConvertJSONToDataTable(jsonString);

       protected DataTable ConvertJSONToDataTable(string jsonString)
    {
        DataTable dt = new DataTable();
        //strip out bad characters
        string[] jsonParts = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");

        //hold column names
        List<string> dtColumns = new List<string>();

        //get columns
        foreach (string jp in jsonParts)
        {
            //only loop thru once to get column names
            string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
            foreach (string rowData in propData)
            {
                try
                {
                    int idx = rowData.IndexOf(":");
                    string n = rowData.Substring(0, idx - 1);
                    string v = rowData.Substring(idx + 1);
                    if (!dtColumns.Contains(n))
                    {
                        dtColumns.Add(n.Replace("\"", ""));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Error Parsing Column Name : {0}", rowData));
                }

            }
            break; // TODO: might not be correct. Was : Exit For
        }

        //build dt
        foreach (string c in dtColumns)
        {
            dt.Columns.Add(c);
        }
        //get table data
        foreach (string jp in jsonParts)
        {
            string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
            DataRow nr = dt.NewRow();
            foreach (string rowData in propData)
            {
                try
                {
                    int idx = rowData.IndexOf(":");
                    string n = rowData.Substring(0, idx - 1).Replace("\"", "");
                    string v = rowData.Substring(idx + 1).Replace("\"", "");
                    nr[n] = v;
                }
                catch (Exception ex)
                {
                    continue;
                }

            }
            dt.Rows.Add(nr);
        }
        return dt;
    }

