dotnet / dotnet/SqlClient

SqlException.Number is always 0 when using .NET framework remoting

Open
#1,940 3 comments 0 reactions 0 assignees View on GitHub
Repro Available :heavy_check_mark:
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

### Describe the bug
When using Remoting in .NET Framework, the SqlException.Number getter always returns 0. This makes it difficult to determine the type of SQL error that occurred. In fact, other event details are also missing, such as LineNumber, Class, and so on.

### To reproduce
In a remoting client/server context, have the client call the server to insert a duplicate record that violates a unique constraint. On the server, it gets the SqlException with the Number=2627. However, when this exception is passed back to the calling client (over the remoting channel), it comes back with Number=0 (and the other error data is missing)

### Expected behavior
Expected behavior is to retain the error number, and other details about the SQL error.

### Further technical details
Microsoft.Data.SqlClient version: 5.1.0
.NET target: Framework 4.8, Netstandard2.0
SQL Server version: SQL Server 2019
Operating system: Windows 11

**Additional context**
I found that in the SqlException class (https://github.com/dotnet/SqlClient/blob/main/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlException.cs), these properties such as Number, LineNumber, etc, get the values from the .Errors collection. I see in the GetObjectData() method it serializes the Errors as null., which would explain why they don't come across the wire. It does, however, put the info into the Data dictionary, so my workaround is to pull the key/value pair from the dictionary to get the error number.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.