You may receive a SqlException error message when your .NET Framework client application calls a stored procedure that has an output parameter of the nvarchar type and the application uses the SqlClient class to work with SQL Server 2000 (896373)



The information in this article applies to:

  • Microsoft SQL Server 2000 Standard Edition
  • Microsoft SQL Server 2000 Personal Edition
  • Microsoft SQL Server 2000 Enterprise Edition
  • Microsoft SQL Server 2000 Desktop Engine (MSDE)
  • Microsoft SQL Server 2000 Developer Edition

Bug #: 469978 (SQL Server 8.0)
SQL Server 8.0:469978

SYMPTOMS

When you use the SqlClient class in a Microsoft .NET Framework client application to work with Microsoft SQL Server 2000 hotfix build 2000.80.811 or a later version, you may receive an error message if the following conditions are true:
  • Your application tries to call a stored procedure.
  • The output parameter of the stored procedure is an nvarchar type.
  • The application sets the output parameter of the stored procedure to a value that is more than 4,000.
The text is of the error message is similar to the following:
System.Data.SqlClient.SqlException: A severe error occurred on the current command. The results, if any, should be discarded.
The following error message also appears in the SQL Server 2000 error log:
Error: 17805, Severity: 20, State: 3
Invalid buffer received from client.

CAUSE

According to SQL Server Books Online, the nvarchar data type is a variable-length Unicode character data of n characters. The placeholder n must be a value from 1 through 4,000. In this scenario, SQL Server resets the client connection if you try to set the size to a value that is more than 4,000 characters. Then, you receive the error message that is mentioned in the "Symptoms" section.

Note SQL Server 2000 hotfix builds that are earlier than 2000.80.811 do not tighten the boundaries of the parameter size of the nvarchar data type.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the problem

  1. Create a stored procedure on the computer that is running SQL Server 2000 by using the following statement:
    CREATE PROCEDURE sp_mytest @result nvarchar(4000) output AS
                SET @result= 'Dummy testing'
  2. Create a Microsoft Visual C# Console Application that contains the following code:

    Note Before you run the code, replace the DBServer and DBName variables with the correct names of your computer that is running SQL Server and your database.
    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    
    namespace SqlTest
    {
    
    	class Class1
    	{
    		private static string DBServer = "SQLServerName";
    		private static string DBName = "DatabaseName";
    
    		[STAThread]
    		static void Main(string[] args)
    		{
    			
    			try
    			{
    
    				string connectionString = "Data Source=" + DBServer+ ";Integrated Security=SSPI;Initial Catalog="+DBName;
    
    				SqlConnection sqlConn = new SqlConnection(connectionString);
    
    				SqlParameter param = null;
    				
    				string cmdText = "sp_mytest";
    				SqlCommand cmd = sqlConn.CreateCommand();
    				cmd.CommandType = CommandType.StoredProcedure;
    				cmd.CommandText= cmdText;
    
    				param = cmd.Parameters.Add("@result",SqlDbType.NVarChar);
    				param.Direction = ParameterDirection.Output;
    				param.Size = 6000;
    				sqlConn.Open();			
    				cmd.ExecuteNonQuery();
    				Console.WriteLine (param.Value.ToString());
    				sqlConn.Close();
    			}
    			catch(SqlException	e)
    			{
    				Console.WriteLine(e.Message );
    				
    			}
    
    
    		}
    	}
    }
  3. Build and then run the Visual C# Console Application. You receive the error messages that are mentioned in the "Symptoms" section.

Modification Type:MajorLast Reviewed:4/7/2005
Keywords:kberrmsg kbprb kbtshoot KB896373 kbAudITPRO kbAudDeveloper