-1

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

VanteGud
  • 7
  • 2
  • the place were it's installed is the user running have permission to sql server? perhaps the permissions have changed in sql. – Jpsh Sep 13 '20 at 19:47
  • An edit to this question not made by OP introduces a very big difference from the original code. In the first revision there is no `` in the connectionstring. Do you have a server name in that place or not? Because if you haven't set a server name it is highly probable that this code cannot work on a machine where Sql Server is not installed – Steve Sep 13 '20 at 20:01
  • 1
    I'm sure you have some misunderstandings here. The null of `SqlConnection` is impossible because you are just creating it and passing to the `ServerConnection`. As I understand your "compiled" program is not properly compiled and it is using some old code. If there would be a permission error as @Jpsh commented above you would get another exception, while connecting to server. – martikyan Sep 13 '20 at 20:05
  • Why would “the SqlConnection object is filled with null's” _not_ be a surprise and/or _how_ is it indicative of an issue? It should contain the non-null connection string (the constructor will throw is it is null; the result of `new` is _never_ null). It should also be in the Closed (?) state. The SqlConnection is only expected to “fail” — in which case an Exception is thrown — when `Open` is called and it actually attempts to connect. – user2864740 Sep 13 '20 at 20:09
  • 1
    Do you run the 'installed' program on the same machine where you run your debug session? – Steve Sep 13 '20 at 20:11
  • Could be a version incompatibility or other issue with SMO, not SqlClient. What version of SMO are you using? Do you really need SMO? Are you using the NuGet package version or the version installed with SQL Server/SSMS? https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects/ – David Browne - Microsoft Sep 13 '20 at 20:33
  • I'm wondering if there's a try/catch hiding an earlier exception. – Joel Coehoorn Sep 14 '20 at 14:17
  • @JoelCoehoorn What effect could that have? The string ConnectionString is getting to the method with the right information. Does SMO initialize something? – VanteGud Sep 14 '20 at 14:28
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Milney Sep 14 '20 at 14:38
  • @Milney - Sadly no. I feel that I parse a valid ConnectionString to the Microsoft.Data.SqlClient.SqlConnection constructor, but I get no actionable errors. just a NullReferenceException. As I cannot debug thru the Microsoft.Data.SqlClient.SqlConnection method, I am stuck. – VanteGud Sep 14 '20 at 14:49
  • Do you have build specific configuration files? You may have a config for release builds that does not have a valid connection string in it. – MikeJ Sep 14 '20 at 15:23
  • @MikeJ - Valid point. I have checked and double-checked. The string (ConnectionString) that hits the method is exactly the same (and on/from the same computer). – VanteGud Sep 14 '20 at 15:46
  • Is the database on prem on in Azure? – MikeJ Sep 14 '20 at 16:07
  • @MikeJ - On Prem. New installation of SQL2019 15.0.4063.15. – VanteGud Sep 14 '20 at 16:20

1 Answers1

0

So, if it helps anyone else, here is my solution;

I turns out that it was something with the assembly Microsoft.Data.SqlClient.dll - or more specifically Microsoft.Data.SqlClient.SNI.x64.dll. I have no idea what faulty version was installed with the program vs. what the debugger was using, but something was not right. (all I could see in the debug sessions was a matching version of 2.0.20168.4 in regards to Microsoft.Data.SqlClient.dll)

I ended up scrapping all use of NuGet and handle the assemblies by myself locally, so I knew exactly what was referenced/used. This resolved the issue.

VanteGud
  • 7
  • 2