Tag Archives: .net

Solution for: A transport-level error has occurred when receiving results from the server…

We have a program written in C# using Visual Studio 2012 and .NET 4.0 / .NET 4.5. It rapidly executes many queries in Microsoft SQL Server 2008 R2 and then exits. Executed query count varies from several hundred to many thousand.

Today, when executed particular task with very many queries, I noticed an error in the log file:
A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
at System.Data.SqlClient.TdsParserStateObject.ReadByte()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at MyTestApp.Program.QueryOneLineOneValue(String cmdText)
at MyTestApp.Program.Worker(String[] args)
at MyTestApp.Program.Main(String[] args)

At first, the error message lead me to think that something is wrong with the network. “Physical connection is not usable”… anyway it didn’t say anything useful… and SQL Server log file didn’t have anything useful either.

A quick Google search revealed that many people have the same problem:

  1. Minimizing Connection Pool errors in SQL Azure — it has code example with an serious bug, and it leads to Reliability Update 1 for the .NET Framework 4 which may help judging from the description – “Issue 14 – A transport-level error has occurred when sending the request to the server.” I didn’t tried it, mostly because it talks about Azure services.
  2. Discussion in the Microsoft SQL Server Database Engine forum which leads to MSDN article about SQL Server Connection Pooling which again does not help much, because I have only one connection in my application.
  3. And a couple not very useful pages from the Stack Overflow: One suggests to call ClearAllPools and another one is about Azure again.

Again, I had to find solution by myself. I looked at the code and didn’t see anything unusual. I did another run under the VS debugger, and after a while the same exception was thrown. Looking under debug log file, I noticed that program stopped in the same place that in the production server. And by the same place I mean, the same amount of SQL commands were executed.

I quickly put a counter on right before SQL command execution and found that .NET throws exception always on 32767 command. 32767 is very familiar constant for programmers, and it is obvious that some resource leakage is happening. A quick glance at the code again revealed that a call to Close or Using statement is missing.

SqlConnection conn;
private static void QueryOneLineOneValue(String cmdText)
      string q = "SELECT ... FROM cmdText...";
      SqlCommand command =
         new SqlCommand(q, conn);
      SqlDataReader reader = command.ExecuteReader();
      while (reader.Read())
         //do something with the data...
      reader.Close(); //this line was missing

C# / .NET MessageBox is hidden behind the Form

Imagine calling the following C# code:MessageBox.Show(this, "Some text...", "Title", MessageBoxButtons.OKCancel);

After executing this line, the Message Box is not visible, it seems like hidden behind your form, without input focus, mouse clicks cause default beeps, and it suddenly appears if you click Alt or F10 key.

It turns out I had a custom drawing routine for one of the form’s controls (OwnerDraw / OnPaint), and I was changing the property that was causing the redraw cycle. Make sure, that in your OnPaint / DrawItem / DrawSubitem / etc. you are not invalidating your Control.

Also, if you are using .NET ListView control, it has a bug.

Because of a bug in the underlying Win32 control, the DrawItem event occurs without accompanying DrawSubItem events once per row in the details view when the mouse pointer moves over the row, causing anything painted in a DrawSubItem event handler to be overpainted by a custom background drawn in a DrawItem event handler. See the example in the OwnerDraw reference topic for a workaround that invalidates each row when the extra event occurs. An alternative workaround is to put all your custom drawing code in a DrawSubItem event handler and paint the background for the entire item (including subitems) only when the DrawListViewSubItemEventArgs.ColumnIndex value is 0.


This is not limited to MessageBox using C# / .NET framework. This also happens very often, with Win32 & WinAPI, with a Form class, etc.:

.NET text box blank/empty/invisible/hidden when text reaches certain length

It seems, that .NET 2.0 have bug when text box (TextBox control) appears blank / empty / invisible / hidden — not showing text when text length reaches certain size. My quick tests shows, that the length of the text that causes this bug to appear depends on something and are changing from program to program or from computer to computer. Unfortunately I do not have resources to test this bug in full right now.

How to repeat this bug in C# .NET 2.0.

Open Visual Studio and create New Project – Windows Forms Project.

Create a new .NET 2.0 project

Add one TextBox and two Buttons to the project.

Form with 2 Buttons and TextBox

Add click handlers for two buttons and one helper function. Here is a code.

      private void button1k_Click(object sender, EventArgs e)
         textBox.Text = GenerateString(1000);
      private void button10k_Click(object sender, EventArgs e)
         textBox.Text = GenerateString(10000);
      private string GenerateString(int lenght)
         string text = "";
         for (int i = 0; i < lenght; ++i)             text += Convert.ToChar('0' + i % 10);          return text;       }

Now run the code! Clicking on the first button, TextBox shows some random numbers as expected.

Correct result, text is shown

Now click on the second button. Text disappears, and only blinking cursor is shown. Also note, that TextBox is functioning -- you can still delete / add characters, select all, use copy / paste, you just do not see the results on the screen.

Incorrect result, only cursor blinks

Workarounds? Currently none. Submitted this bug to Connect: .NET 2.0 TextBox fails to display long texts.

Tested on Windows 7 Pro 64-bit and Windows 7 Ultimate 64-bit with latest updates on 12/2/2011. Compiled with latest Visual Studio 2010 Pro 10.0.40219.1 SP1Rel w/ Microsoft .NET Framework Version 4.0.30319 SP1Rel.

P.S. One similar bug is reported on Microsoft Connect, however it seems like separate issue: Vanishing text bug.

P.S.S. My source code can be downloaded from MS Connect.