Microsoft KB Archive/827366

= &quot;Invalid Buffer Received from Client” error message in SQL Server log when you use SQL Server .NET provider classes =

Article ID: 827366

Article Last Modified on 4/18/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



SYMPTOMS
When you use a Microsoft .NET Framework SqlClient class, you receive the following error message in the Microsoft SQL Server 2000 error log:

Error: 17805, Severity: 20, State: 3

Invalid buffer received from client.

You receive the following corresponding error messages in the Microsoft .NET Framework client application:

System.Data.SqlClient.SqlException: A severe error occurred on the current command. The results, if any, should be discarded



CAUSE
This behavior occurs if one of the following scenarios is true:
 * You use a SqlClient class in a Finalize method or in a C# destructor.
 * You do not specify an explicit SQLDbType enumeration when you create a SqlParameter object. When you do not specify an explicit SQLDbType enumeration, the Microsoft .NET Framework Data Provider for SQL Server (SqlClient) tries to select the correct SQLDbType enumeration based on the data that is passed. SqlClient is unsuccessful.
 * You declare an instance of the SqlClient class to be static in C# or to be shared in Visual Basic. The instance can be accessed concurrently from more than one thread in the application.

Note In the ASP.NET environment or in some other environments, concurrent access is possible even if no additional threads are created explicitly in the application code.
 * The size of the parameter that you explicitly specify in the .NET Framework code is more than the maximum size that you can use for the data type in SQL Server.

For example, according to SQL Server Books Online, nvarchar is a variable-length Unicode character data of n characters. &quot;n&quot; must be a value from 1 through 4000. If you specify a size that is more than 4000 for an nvarchar parameter, you receive the error message that is described in the &quot;Symptoms&quot; section.

The following code also demonstrates how these errors might occur: Stored Procedure --

CREATE PROCEDURE spParameterBug @myText Text AS           Insert Into ParameterBugTable (TextField) Values (@myText)

Code --- static void Main(string[] args) {      string dummyText=string.Empty; for (int n=0; n < /*80*/ 3277; n++)  // change this to 80 to receive the second error that is mentioned earlier in this article. {            dummyText += &quot;0123456789&quot;; }

// TO DO: Change data source to match your SQL Server:

SqlConnection con= new SqlConnection(&quot;data source=myserver;Initial Catalog=mydb;Integrated Security=SSPI;persist security info=True;packet size=16384&quot;); SqlCommand cmd = new SqlCommand(&quot;SpParameterBug&quot;, con); con.Open; // Causes error 17805: SqlParameter param2 =new SqlParameter(&quot;@myText&quot;, dummyText); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(param2); try {         cmd.ExecuteNonQuery; }      catch (Exception err) {        Console.WriteLine(err.ToString); }

Console.ReadLine;

}



RESOLUTION
To resolve these errors, make sure that you do the following:
 * Do not use a SqlClient class in a Finalize method or in a C# destructor.
 * Specify the SqlDbType enumeration for the SqlParameter object so that there is no inferred type.
 * Specify a parameter size that is within the limits of the data type.

