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 when 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, the Microsoft .NET Framework Data Provider for SQL Server
(SqlClient) tries to select the correct SQLDbType based on the data that is passed. SqlClient is not successful.
- 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 Microsoft SQL Server.
For example, according to SQL
Server Books Online, nvarchar is a variable-length Unicode character data of n characters. "n"
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
"Symptoms" 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 += "0123456789";
}
// TO DO: Change data source to match your SQL Server:
SqlConnection con= new SqlConnection("data source=myserver;Initial Catalog=mydb;Integrated Security=SSPI;persist security info=True;packet size=16384");
SqlCommand cmd = new SqlCommand("SpParameterBug", con);
con.Open();
// Causes error 17805:
SqlParameter param2 =new SqlParameter("@myText", dummyText);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(param2);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
Console.ReadLine();
}