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
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. "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();
}
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.
For more information about the maximum size for different
data types, visit the following Microsoft Developer Network (MSDN) Web sites: