Skip to content

[Bug]: Stored procedures with XML output generates ArgumentOutOfRangeException #4774

Description

@FarhanGhumra

What happened?

I have a stored procedure which returns the XML as an output parameter. When I call the procedure from a C# application, it generates ArgumentOutOfRangeException. Refer below given exception stacktrace. The C# application uses Microsoft.Data.SqlClient 6.1.3 NuGet package for database operations. To regenerate the issue, refer below given sample stored procedure and its execution using C#.

Stored procedure

CREATE PROCEDURE [dbo].[spu_XmlOutputTest]
    @UsersXml XML Output
AS
BEGIN
    SET NOCOUNT ON
    SET @UsersXml = N'<root><p n="EventAndAuditUser" v="Foo System Agent" /><p n="EventAndAuditUser" v="Bar" /></root>'
END

Stored procedure execution using C#

private void XmlOutputTest1()
{
    try
    {
        XmlDocument xmlResult = new XmlDocument();

        SqlConnection sqlConnection = new SqlConnection("dbConnectionString");
        sqlConnection.Open();
        
        SqlCommand sqlCommand = new SqlCommand();
        sqlCommand.Connection = sqlConnection;
        sqlCommand.Parameters.Clear();
        sqlCommand.CommandText = "spu_XmlOutputTest";
        sqlCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter sqlParam = new SqlParameter("@UsersXml", SqlDbType.Xml, 100000);
        sqlParam.Direction = ParameterDirection.Output;
        
        sqlCommand.Parameters.Add(sqlParam);

        xmlResult.Load(sqlCommand.ExecuteXmlReader());
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Exception

System.ArgumentOutOfRangeException: The SqlDbType enumeration value, 0, is invalid. (Parameter 'SqlDbType')
   at Microsoft.Data.SqlClient.MetaType.GetSqlDataType(Int32 tdsType, UInt32 userType, Int32 length)
   at Microsoft.Data.SqlClient.TdsParser.TryProcessReturnValue(Int32 length, TdsParserStateObject stateObj, SqlReturnValue& returnValue, SqlCommandColumnEncryptionSetting columnEncryptionSetting)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteXmlReader()
   at BabelfishDiag.Program.XmlOutputTest() in C:\PoC\BabelfishDiag\BabelfishDiag\Program.cs:line 159

Version

BABEL_5_X_DEV (Default)

Extension

babelfishpg_tds

Which flavor of Linux are you using when you see the bug?

Ubuntu (Default)

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions