<p>I have a WCF service from which I want to return a DataTable. I know that this is often a highly-debated topic, as far as whether or not returning DataTables is a good practice. Let's put that aside for a moment.</p>

<p>When I create a DataTable from scratch, as below, there are no problems whatsoever. The table is created, populated, and returned to the client, and all is well:</p>

<pre><code>[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    for(int i=0;i&lt;100;i++)
    {
        tbl.Columns.Add(i);
        tbl.Rows.Add(new string[]{"testValue"});
    }
    return tbl;
}
</code></pre>

<p>However, as soon as I go out and hit the database to create the table, as below, I get a CommunicationException "The underlying connection was closed: The connection was closed unexpectedly."</p>

<pre><code>[DataContract]
public DataTable GetTbl()
{
    DataTable tbl = new DataTable("testTbl");
    //Populate table with SQL query

    return tbl;
}
</code></pre>

<p>The table is being populated correctly on the server side. It is significantly smaller than the test table that I looped through and returned, and the query is small and fast - there is no issue here with timeouts or large data transfer. The same exact functions and DataContracts/ServiceContracts/BehaviorContracts are being used.</p>

<p>Why would the way that the table is being populated have any bearing on the table returning successfully?</p>

[{'answer_id': 12712, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>The attribute you want is OperationContract (on interface) / Operation Behavior (on method):</p>\n\n<pre><code>[ServiceContract]\npublic interface ITableProvider\n{\n    [OperationContract]\n    DataTable GetTbl();\n}\n\n\n[OperationBehavior]\npublic DataTable GetTbl(){\n    DataTable tbl = new DataTable("testTbl");\n    //Populate table with SQL query\n\n    return tbl;\n}\n</code></pre>\n\n<p>Also, in the... I think service configuration... you want to specify that errors can be sent. You might be hitting an error that is something like the message size is to big, etc. You can fix that by fudging with the reader quotas and such.</p>\n\n<p>By default wsHttpBinding has a receive size quota of like 65&nbsp;KB, so if the serialized data table\'s XML is more than that, it would throw an error (and I\'m 95% sure the data table is more than 65&nbsp;KB with data in it).</p>\n\n<p>You can change the settings for the reader quotas and such in the <code>web.config</code> / <code>app.config</code> or you can set it on a binding instance in code. But yeah, that\'s probably what your problem is, if you haven\'t changed it by default.</p>\n\n<p><em><a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.wshttpbindingbase_members.aspx" rel="nofollow noreferrer">WSHttpBindingBase Members</a></em> - Look at ReaderQuotas property as well as the MaxReceivedMessageSize property.</p>\n'}, {'answer_id': 24492, 'author': 'Chris Gillum', 'author_id': 2069, 'author_profile': 'https://Stackoverflow.com/users/2069', 'pm_score': 4, 'selected': False, 'text': '<p>The best way to diagnose these kinds of WCF errors (the ones that really don\'t tell you much) is to enable tracing.  In your web.config file, add the following:</p>\n\n<pre><code>  &lt;system.diagnostics&gt;\n    &lt;sources&gt;\n      &lt;source name="System.ServiceModel" \n              switchValue="Information" \n              propagateActivity="true"&gt;\n        &lt;listeners&gt;\n          &lt;add name="ServiceModelTraceListener" \n               type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" \n               initializeData="wcf-traces.svclog"/&gt;\n        &lt;/listeners&gt;\n      &lt;/source&gt;\n    &lt;/sources&gt;\n  &lt;/system.diagnostics&gt;\n</code></pre>\n\n<p>You can then open the resulting file in the SvcTraceViewer.exe utility which comes in the .NET Framework SDK (or with Visual Studio).  On my machine, it can be found at %PROGRAMFILES%\\Microsoft SDKs\\Windows\\v6.0A\\Bin\\SvcTraceViewer.exe.</p>\n\n<p>Just look for an error message (in bold red) and that will tell you specifically what your problem is.</p>\n'}, {'answer_id': 39638, 'author': 'Paul Mrozowski', 'author_id': 3656, 'author_profile': 'https://Stackoverflow.com/users/3656', 'pm_score': 0, 'selected': False, 'text': "<p>I think Darren is most likely correct - the default values provided for WCF are laughably small and if you bump into them you end up with errors that can be difficult to track down. They seem to appear as soon as you attempt to do anything beyond a simple test case. I wasted more time than I'd like to admit debugging problems that turned out to be related to the various configuration (size) settings on both the client and server. I think I ended up modifying almost all of them, ex. MaxBufferPoolSize, MaxBufferSize, MaxConnections, MaxReceivedMessageSize, etc. </p>\n\n<p>Having said that, the SvcTraceViewer utility also mentioned is great. I did run into a few cases where it wasn't as helpful as I would have liked, but overall it's a nice tool for analyzing the communications flow and errors.</p>\n"}, {'answer_id': 42332, 'author': 'goric', 'author_id': 940, 'author_profile': 'https://Stackoverflow.com/users/940', 'pm_score': 7, 'selected': True, 'text': '<p>For anyone having similar problems, I have solved my issue.  It was several-fold.</p>\n\n<ul>\n<li>As Darren suggested and Paul backed up, the Max..Size properties in the configuration needed to be enlarged.  The SvcTraceViewer utility helped in determining this, but it still does not always give the most helpful error messages.  </li>\n<li>It also appears that when the Service Reference is updated on the client side, the configuration will sometimes not update properly (e.g. Changing config values on the server will not always properly update on the client. I had to go in and change the Max..Size properties multiple times on both the client and server sides in the course of my debugging)</li>\n<li><p>For a DataTable to be serializable, it needs to be given a name.  The default constructor does not give the table a name, so:</p>\n\n<pre><code>return new DataTable();\n</code></pre>\n\n<p>will not be serializable, while:</p>\n\n<pre><code>return new DataTable("someName");\n</code></pre>\n\n<p>will name the table whatever is passed as the parameter. </p>\n\n<p>Note that a table can be given a name at any time by assigning a string to the <code>TableName</code> property of the DataTable.</p>\n\n<pre><code>var table = new DataTable();\ntable.TableName = "someName";\n</code></pre></li>\n</ul>\n\n<p>Hopefully that will help someone.</p>\n'}, {'answer_id': 233550, 'author': 'Sam', 'author_id': 7021, 'author_profile': 'https://Stackoverflow.com/users/7021', 'pm_score': 2, 'selected': False, 'text': '<p>You probably blew your quota - the datatable is larger than the allowed maximum packet size for your connection.</p>\n\n<p>You probably need to set <strong>MaxReceivedMessageSize</strong> and <strong>MaxBufferSize</strong> to higher values on your connection.</p>\n'}, {'answer_id': 4499790, 'author': 'aditya pathak', 'author_id': 549975, 'author_profile': 'https://Stackoverflow.com/users/549975', 'pm_score': 3, 'selected': False, 'text': '<p>Other than setting maximum values for all binding attributes.</p>\n\n<p>Make sure each table you are passing/returning from webservice must have a table name, meaning the <code>table.tablename</code> property should not be blank.</p>\n'}, {'answer_id': 7872975, 'author': 'Jani5e', 'author_id': 1008160, 'author_profile': 'https://Stackoverflow.com/users/1008160', 'pm_score': 3, 'selected': False, 'text': '<p>I added the Datable to a data set and returned the table like so...</p>\n\n<pre><code>DataTable result = new DataTable("result");\n\n//linq to populate the table\n\nDataset ds = new DataSet();\nds.Tables.Add(result);\nreturn ds.Tables[0];\n</code></pre>\n\n<p>Hope it helps\n:)</p>\n'}, {'answer_id': 40925254, 'author': 'Mukesh Methaniya', 'author_id': 1410033, 'author_profile': 'https://Stackoverflow.com/users/1410033', 'pm_score': 1, 'selected': False, 'text': '<p>There are 3 reason for failed return type as <code>datatable</code> in WCF services</p>\n\n<ul>\n<li><p>You have to specify data table name like:  </p>\n\n<pre><code>MyTable=new DataTable("tableName");\n</code></pre></li>\n<li><p>When you are adding reference on client side of WCF service select reusable dll <code>system.data</code></p></li>\n<li><p>Specify attribute on <code>datatable</code> member variable like</p>\n\n<pre><code>[DataMember]\npublic DataTable MyTable{ get; set; }\n</code></pre></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/12702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/940/']