Issue While Returning DataTable Through WCF Service
Problem
In the previous article, I faced a common problem when returning a DataTable through a WCF Service. I'm receiving that error message when using one of my webmethods on my WCF webservice.
"The underlying connection was closed: The connection was closed unexpectedly."
This error only occurs when returning a DataTable. If I return some primitive type or even a Dictionary<string, string>, it returns no problem.
Iservice1.cs File
Now we create a function in the OperationContract section of the Iservice1.cs file:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.Data ;
- namespace WCFServiceWith_ReturnTable
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- DataTable SelectUserDetails();
- }
- // Use a data contract as illustrated in the sample below to add composite types to service operations.
- [DataContract]
- public class UserDetails
- {
- [DataMember]
- public DataTable UserRecord
- {
- get;
- set;
- }
- }
- }
In this file we define the function SelectUserDetails(UserDetails userInfo).
And replace the code with the following:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- namespace WCFServiceWith_ReturnTable
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
- public class Service1 : IService1
- {
- public DataTable SelectUserDetails()
- {
- SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;User ID=sa;Password=wintellect");
- con.Open();
- SqlCommand cmd = new SqlCommand("Select * from RegistrationTable", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- cmd.ExecuteNonQuery();
- con.Close();
- return dt;
- }
- }
- }

Write following code in the web Application:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using ServiceReference1;
- using System.IO;
- using System.Data;
- using System.Xml;
- public partial class _Default : System.Web.UI.Page
- {
- ServiceReference1.Service1Client objServiceClientobjService = new ServiceReference1.Service1Client();
- protected void Page_Load(object sender, EventArgs e)
- {
- DataTable dt = new DataTable();
- dt=objServiceClientobjService.SelectUserDetails();
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- }

Returning DataTable in WCF
This is a serialization issue with WCF. Your Datacontract contains a type which the DataContractSerializer is not able to serialize. Here, we will introduce how to return a DataTable using a WCF service from C# code. To return a DataTable using a WCF service, we must do the following 3 things:
- Create Database Table
- Create WCF Service
- Create a Web Application
In the first step we will create a table in SQL Server; after that we create a simple function to return the table from the database using a WCF service. In a web application, add a reference of the service and data to be returned which will be sent to the web services function. Let's take a look at a practical example. The example application is developed in Visual Studio 2010 and SQL Server 2008.
Step 1: Creating Database Table
- Database name: Registration
- Database table name: RegistrationTable
RegistrationTable Table
- CREATE TABLE [dbo].[RegistrationTable]
- (
- [UserName] [varchar](100) NOT NULL,
- [Password] [varchar](20) NOT NULL,
- [Country] [varchar](100) NOT NULL,
- [Email] [varchar](200) NOT NULL
- )













sanjib mannaPosted Feb 8, 2018, 1:12 PM
Still facing the same exception.