Skip to content

Connection not working but not throwing exceptions #3868

@rngkushner

Description

@rngkushner

Describe the bug

I upgraded to Microsoft.Data.SqlClient 6.1.3 and now getting "Object reference not set to an instance of an object" on every data call.

The SQLConnection object initializes but when checking connection.State or connection.ConnectionString, it throws a null reference. I believe this is the issue.

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.get_ConnectionString()

In the appsettings.json:
"db": "Data Source=MyDB;Initial Catalog=MyDB; user id=MyUser; Password=MyPassword;TrustServerCertificate=True"

Project settings

net6.0
disable
enable

``` Exception message: Stack trace:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlDataAdapter.System.Data.IDbDataAdapter.get_SelectCommand()


### To reproduce


```c#
connection = new SqlConnection(GetConnectionStr());

connection.Open(); // error occurs testing any property in Immediate window, so I think the error is here.

string SQL = string.Format("select * from {0}", tableName);
SqlCommand cmd = new SqlCommand(SQL, connection);
 cmd.CommandType = CommandType.Text;
DataTable retn = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(retn);

Expected behavior

It should fill the DataTable as it performed many times before.

Further technical details

Microsoft.Data.SqlClient version: 6.1.3
.NET target: NET 6.0, DotNetCoreApp 6.0.36, AspNetCore 6.0.36
SQL Server version: 13.0.7070.4
Operating system: Windows 11

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions