System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.

The problem:

You getting are getting the following error (depending on what SQL Server Version you are running).

If you are running against Sql Server 2000:

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

A severe error occurred on the current command. The results, if any, should be discarded.

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()

   ...

If you are running against Sql Server 2005:

System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.

Parameter 1 ("@p1"): Data type 0xE7 has an invalid data length or metadata length.

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)

   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()

   ...

You may also have the following in the Event Log and/or in SQL Profiler:

Error: 17805, Severity: 20, State: 3

Invalid buffer received from client.

Having this passion for repros (if you log a support case with me and you have a repro, you will not only be considered to be my best friend,

it will also almost guarantee that I can help you quicker) I will again show by example:

1. Connect to your Sql Server 2000 or 2005.

2. In Query Analyzer or Sql Server Management Studio, create a table, insert a row and create a stored procedure, like this:

-- Create table and insert a row

create table TdsErrorTable (colId int primary key identity, colName nvarchar(100))

insert into TdsErrorTable values ('abc')

go

-- Create procedure

create procedure TdsErrorProcedure(@p1 nvarchar(100)) as

select colId from TdsErrorTable where colName like @p1

go

-- Test procedure, should return 1

exec TdsErrorProcedure 'abc'

3. Create a new C# console application in Visual Studio, edit the Main method to look like this:

        static void Main(string[] args)

        {

            string cString = @"Data Source=<your server>;Initial Catalog=Repros;Integrated Security=True";

    try

            {

                using (SqlConnection sc = new SqlConnection(cString))

                {

                    sc.Open();

                    SqlCommand cmd = new SqlCommand("TdsErrorProcedure", sc);

                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.Add("@p1", System.Data.SqlDbType.NVarChar, 4000).Value = "abc";

                    // Uncomment the line below and comment out the line above, this will give the error

     //cmd.Parameters.Add("@p1", System.Data.SqlDbType.NVarChar, 4001).Value = "abc";

                    int i = (int)cmd.ExecuteScalar();

                    Console.WriteLine("Result -> {0}", i);

                    sc.Close();

              }

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

            }

        }

4. Run it, this should return "Result -> 1".

5. Now uncomment the line where the NVarChar length is 4001 and comment out where it is 4000

6. Run the application again, it should now terminate with:

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

A severe error occurred on the current command. The results, if any, should be discarded.

Or, if running SQL Server 2005:

System.Data.SqlClient.SqlException: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect.

Parameter 1 ("@p1"): Data type 0xE7 has an invalid data length or metadata length.

7. Optionally, check the SQL Profiler or the Event Log on the server, you may have:

Error: 17805, Severity: 20, State: 3

Invalid buffer received from client.

This clearly shows that the error comes from the fact that the provided parameters length is too long.

Two things to check for:

.1 Make sure that the parameter length is not too long for the data type.

.2 It could be that a column in the database has changed data type, for example if a migration has been made, then in the old and working server you may have had

columns of type Varchar and in the new one these have been set to NVarchar.

  

   Remember that NVarchar is variable-length Unicode character data of n characters.

   Therefore storage size, in bytes, is two times the number of characters entered.

So for Varchar you can have up to 8000 chars but in Nvarchar you can only have 4000 (n*2)

Hopefully this gives some clues on what to look for when getting this error.

Reference:

“Using Unicode Data”

https://msdn.microsoft.com/en-us/library/aa223981(SQL.80).aspx