Repository navigation
Description
Description
I am converting a commercial product from .NET Framework 4.7 to .NET 8. Some of our unit tests are failing and the cause seems to be an unexpected behaviour difference in the System.Data.OleDb.OleDbException object. After deliberately executing invalid SQL, the Errors property on the resulting exception is an empty collection instead of containing the expected OleDbError object.
In production, we use the OleDbError.NativeError number to handle errors coming from SQL Server differently depending on e.g. whether the error reflects a temporary connection issue as opposed to a syntax error.
Reproduction Steps
Install Visual Studio with the .NET desktop and data storage workloads
Create a VB.NET console project
Add the System.Data.OleDb 8.0.0 package and import the namespace
Add the following code to Main():
Using objConn As New OleDbConnection()
objConn.ConnectionString = "Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="""";Data Source=(localdb)\MSSQLLocalDB;Initial File Name="""";Server SPN="""""
objConn.Open()
Using dbcComm As New OleDbCommand("select * from NonExistentTable", objConn)
Try
dbcComm.ExecuteNonQuery()
Catch ex As OleDbException
Console.WriteLine($"{ex.Errors(0).NativeError}: {ex.Errors(0).Message}")
End Try
End Using
End UsingTwo test case solutions (one for .NET 8, one for .NET Framework 4.7.2):
OleDbErrorTestCase.zip
Expected behavior
Should output the following line:
208: Invalid object name 'NonExistentTable'.
Actual behavior
Throws an exception because Errors is an empty collection:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Source=System.Private.CoreLib
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Data.OleDb.OleDbErrorCollection.get_Item(Int32 index)
at OleDbErrorTestCase_NET8.Program.Main(String[] args) in C:\Users\jkerk\source\repos\OleDbErrorTestCase_NET8\OleDbErrorTestCase_NET8\Program.vb:line 12
Regression?
Same code works as expected in .NET Framework 4.7.2.
Known Workarounds
No response
Configuration
Visual Studio Enterprise 2022 17.9.5
.NET 8.0
System.Data.OleDb 8.0.0
Windows 10 x64
Same behaviour was observed with a SQL Server 2019 instance so I don't think the use of MSSQLLocalDB in the test case is relevant.
Other information
The observed behaviour is contrary to the documentation, which states "The OleDbErrorCollection class always contains at least one instance of the OleDbError class."
Activity
dotnet-policy-service commentedon Apr 1, 2024
Tagging subscribers to this area: @roji, @ajcvickers
See info in area-owners.md if you want to be subscribed.
danmoseley commentedon Apr 1, 2024
@DopefishJustin Would it be possible for you to demo a little bit in each case to see where the behavior diverges? VS should resolve all symbols and sources.
dotnet-policy-service commentedon Aug 27, 2024
Tagging subscribers to this area: @roji, @ajcvickers
See info in area-owners.md if you want to be subscribed.
MischaJ commentedon Jan 8, 2026
We have the exact same issue: after porting our software from .NET Framework 4.7.2 to .NET 8, code that relies on the contents of the OleDbException's Error property fails because it is now empty, where in the fw-version it used to contain relevant info:
Errors[0]: "The statement has been terminated."
Errors[1]: "The INSERT statement conflicted with the FOREIGN KEY constraint F. The conflict occurred in database D, table T, column C."
sabanr commentedon Apr 21, 2026
This issue is still happening to us. We are wrapping the sql statements in a try/catch and sending the error a resultset as a workaround.