Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.2.0: using sqlConnection.FireInfoMessageEventOnUserErrors = true generates "Object reference not set to an instance of an object." exception #2388

Closed
BertDeHaes opened this issue Mar 5, 2024 · 3 comments · Fixed by #2399

Comments

@BertDeHaes
Copy link

Describe the bug

When using sqlConnection.FireInfoMessageEventOnUserErrors = true, a "print" sql command always throws an exception

Exception message:
Object reference not set to an instance of an object.

Stack trace:
at Microsoft.Data.SqlClient.SqlCommand.GetCurrentBatchCommand()
at Microsoft.Data.SqlClient.TdsParser.FireInfoMessageEvent(SqlConnection connection, SqlCommand command, TdsParserStateObject stateObj, SqlError error)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at SqlClientBug.Program.Main(String[] args) in E:\NAS2\COBEMAR\2022\dotNet\SqlClientBug\Program.cs:line 31

To reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

namespace SqlClientBug
{
   internal class Program
    {
        static void Main(string[] args)
        {
            SqlConnectionStringBuilder sb = new();

            sb.TrustServerCertificate = true;
            sb.DataSource = ".";
            sb.InitialCatalog = "tempdb";
            sb.IntegratedSecurity = true;
            sb.ApplicationName = "SqlClientBug";
            
            using SqlConnection sqlConnection = new(sb.ConnectionString);
            sqlConnection.FireInfoMessageEventOnUserErrors = true;
            sqlConnection.Open();
            using SqlCommand cmd = sqlConnection.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.CommandText = "print 'OK';";
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                Console.Error.WriteLine("--------------------");
                Console.Error.WriteLine(ex.StackTrace);
            }
        }
    }
}

Expected behavior

As in previous versions: no Exception

Further technical details

Microsoft.Data.SqlClient version: 5.2.0
.NET target: .NET 8.0
SQL Server version: SQL Server 2022
Operating system: Windows 11

Additional context
No problem when using Microsoft.Data.SqlClient version 5.1.5

@BertDeHaes BertDeHaes changed the title using sqlConnection.FireInfoMessageEventOnUserErrors = true SqlClient version: 5.2.0: using sqlConnection.FireInfoMessageEventOnUserErrors = true "Object reference not set to an instance of an object." exception Mar 5, 2024
@BertDeHaes BertDeHaes changed the title SqlClient version: 5.2.0: using sqlConnection.FireInfoMessageEventOnUserErrors = true "Object reference not set to an instance of an object." exception 5.2.0: using sqlConnection.FireInfoMessageEventOnUserErrors = true generates "Object reference not set to an instance of an object." exception Mar 5, 2024
@PiotrZimniakSP
Copy link

We have the same problem.
Additional information:
The error occurs only when commandType != procedure and only when the passed query returns a warning or error.
Generally, from what we have observed, the error is caused by the uninitialized array _rpcArrayOf1 in SQLCommand class, which is used in this case to return SQLBatchCommand for the SqlException.CreateException method.
The auxiliary query on which this error occurs:
SELECT COUNT(Floor) C FROM (VALUES (1), (NULL), (2)) X (Floor)

@mariusbancila
Copy link

I have the same problem. When should we expect a release that we can use?

@JRahnama
Copy link
Member

I have the same problem. When should we expect a release that we can use?

hotfix release v5.2.1 release date yet TBD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
4 participants