I'm facing really strange issue. Given the code below:
static void Main()
{
var c = new System.Data.SqlClient.SqlConnection();
c.ConnectionString = "Data Source=SOME_NAME;Initial Catalog=SOME_DB;Integrated Security=True";
c.ConnectionString = ""; //null also triggers exception
Console.WriteLine("Success");
}
It worked fine for quite a time, but on newest version of Windows 10 (1803) which has .NET Version 4.7.03056 Release 461808 (seems to be 4.7.2) it crashes with following exception:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.SqlClient.SqlConnection.CacheConnectionStringProperties()
at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at TestCacheConnectionStringProperties.Program.Main()
This crashes on the second assignment, if I remove any of the assignments of the ConnectionString
it works ok.
I've looked at the sources and didn't find a place where NullReferenceException
could happen (however sources seems to be for .NET Framework 4.7.1 so smth may change).
Now the question is - what causes this issue? Is this a .NET bug? If yes - how to address it?
UPDATE: According to the comments - thanks very much guys - the issue is caused by these lines (decompiled):
private void CacheConnectionStringProperties()
{
SqlConnectionString connectionOptions = this.ConnectionOptions as SqlConnectionString;
if (connectionOptions != null)
this._connectRetryCount = connectionOptions.ConnectRetryCount;
//Seems like this is causing the bug because it is not inside of null-check-if for connectionOptions variable
if (this._connectRetryCount != 1 || !ADP.IsAzureSqlServerEndpoint(connectionOptions.DataSource))
return;
this._connectRetryCount = 2;
}
It is somehow related to Azure and is quite different from what is available in sources.
I've posted the issue here and will wait for response.
You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.
The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.
This error is caused when an object is trying to be used by a script but does not refer to an instance of an object. To fix this example we can acquire a reference to an instance of the script using GameObject.
This is a documented known issue in System.Data in .NET Framework 4.7.2 https://github.com/Microsoft/dotnet/blob/master/releases/net472/dotnet472-known-issues.md
The issue is currently being addressed and will be available in future updates of .NET Framework 4.7.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With