I have a C# program that worked, but after an upgrade to VS 2019 / .NET 4.8, the installed program suddenly started to fail. After trying various downgrades and x86, x64 tests, etc. I finally tried to debug the running code, and the error seems to be this:
private static ServerConnection GetServerConnection( string ConnectionString )
{
Microsoft.Data.SqlClient.SqlConnection Test = new Microsoft.Data.SqlClient.SqlConnection(ConnectionString);
ServerConnection conn = new ServerConnection(Test);
try
{
...
}
}
When I debug the code then the SqlConnection
object is created ok/connected. But when I run the compiled program, the SqlConnection
object is filled with null's, then the ServerConnection
fails with a NullException
(No surprise). I use a parameter for the connection string, and it works - And it is exactly the same connection string in the installed program (I know because I ran a debug session on the "running program" (attached a debugger).
Does anyone have an idea as to what is wrong? I am at a loss as to how to debug the code further. What can be changed between the debug session and the running/installed program?
The SQL Server I'm connecting to is version 2019 (tried a 2016 instance too, same problem)
The connection string:
Data Source=<ServerName>;Initial Catalog=master;Integrated Security=True;
Edit to answer comments: @jpsh: I am sysadmin on the SQL Instance and admin on the machine where the program is installed. I would expect some kind of "Access denied" if that could be the case?
@Steve: I am using an actual ServerName, just edited it out. (And the running program uses the same). I assure you, that's not the problem.
@Karen & @user2864740 : I have posted the locals of the 'Test' of the two diffrent SqlConnection objects - Only difference is where it is run. Debug/Release from VS or running program. Sorry if the "Filled with Null's was inaccurate.
When running Debug or Release
- ConnectionString "Data Source=(ActualCName);Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" string
- Test {Microsoft.Data.SqlClient.SqlConnection} Microsoft.Data.SqlClient.SqlConnection AccessToken null string
- CanRaiseEvents true bool ClientConnectionId {00000000-0000-0000-0000-000000000000} System.Guid
- ConnectionString "Data Source=(ActualCName);Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" string
- ConnectionTimeout 30 int
- Container null System.ComponentModel.IContainer
- Credential null Microsoft.Data.SqlClient.SqlCredential
- DataSource "(ActualCName)" string
- Database "master" string DbProviderFactory {Microsoft.Data.SqlClient.SqlClientFactory} System.Data.Common.DbProviderFactory {Microsoft.Data.SqlClient.SqlClientFactory}
- DesignMode false bool
- Events {System.ComponentModel.EventHandlerList} System.ComponentModel.EventHandlerList
- FireInfoMessageEventOnUserErrors false bool
- PacketSize 8000 int
- ServerVersion 'Test.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
- Site null System.ComponentModel.ISite
- State Closed System.Data.ConnectionState
- StatisticsEnabled false bool
- WorkstationId "EUIBIWKS04" string
Static members
Non-Public members - conn null Microsoft.SqlServer.Management.Common.ServerConnection
When running the application:
- Test {Microsoft.Data.SqlClient.SqlConnection} Microsoft.Data.SqlClient.SqlConnection
- AccessToken 'Test.AccessToken' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException} CanRaiseEvents true bool
- ClientConnectionId 'Test.ClientConnectionId' threw an exception of type 'System.NullReferenceException' System.Guid {System.NullReferenceException}
- ConnectionString 'Test.ConnectionString' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException}
- ConnectionTimeout 'Test.ConnectionTimeout' threw an exception of type 'System.NullReferenceException' int {System.NullReferenceException} Container null System.ComponentModel.IContainer
- Credential 'Test.Credential' threw an exception of type 'System.NullReferenceException' Microsoft.Data.SqlClient.SqlCredential {System.NullReferenceException}
- DataSource 'Test.DataSource' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException}
- Database 'Test.Database' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException}
- DbProviderFactory 'Test.DbProviderFactory' threw an exception of type 'System.NullReferenceException' System.Data.Common.DbProviderFactory {System.NullReferenceException} DesignMode false bool
- Events {System.ComponentModel.EventHandlerList} System.ComponentModel.EventHandlerList
- FireInfoMessageEventOnUserErrors 'Test.FireInfoMessageEventOnUserErrors' threw an exception of type 'System.NullReferenceException' bool {System.NullReferenceException}
- PacketSize 'Test.PacketSize' threw an exception of type 'System.NullReferenceException' int {System.NullReferenceException}
- ServerVersion 'Test.ServerVersion' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException} Site null System.ComponentModel.ISite
- State 'Test.State' threw an exception of type 'System.NullReferenceException' System.Data.ConnectionState {System.NullReferenceException}
- StatisticsEnabled 'Test.StatisticsEnabled' threw an exception of type 'System.NullReferenceException' bool {System.NullReferenceException}
- WorkstationId 'Test.WorkstationId' threw an exception of type 'System.NullReferenceException' string {System.NullReferenceException}
@David Browne: I am using SMO to check/update/correct a database. It's seems to be the best tool for the job, but if you have other suggestions, then I am all ears :) I checked, and I use the mentioned version of SMO from NuGet.
The problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: Ui.Console
Problem Signature 02: 1.1.0.1
Problem Signature 03: 5f5e4a0a
Problem Signature 04: Microsoft.Data.SqlClient
Problem Signature 05: 2.0.20168.4
Problem Signature 06: b0b81829
Problem Signature 07: 120
Problem Signature 08: 0
Problem Signature 09: System.NullReferenceException
OS Version: 6.3.9600.2.0.0.400.8
Locale ID: 1030
Additional Information 1: ca7f
Additional Information 2: ca7f81a57f0d6c81935dea0aa57fa2df
Additional Information 3: 947f
Additional Information 4: 947f2c7f58b90d4f468320329b80ef49