Cannot add assembly in VS 2022 C#

Imac0818 6 Reputation points
2022-01-15T20:27:21.727+00:00

I did a clean install of Windows 11 then installed Visual studio 2022 for .NET desktop environment (C@, Visual Basic, F# with NET and NET Framework). Then I created a project of type windows forms app (.net windows forms (winforms) app) with C# filter. I loaded code from an existing project from VS 2019. It was public Microsoft.Data.SqlClient.SqlConnection dbConn; but I changed to public System.Data.SqlClient.SqlConnection dbConn;
I get this error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
I tried looking at Nuget and found nothing. I also looked at Solution Explorer and there are no references. How can I fix this error?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,419 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Castorix31 82,206 Reputation points
    2022-01-15T20:34:34.783+00:00

    Then I created a project of type windows forms app (.net windows forms (winforms) app) with C# filter.

    You probably created a .NET Core project
    Create a .NET Framework project

    (or add the System.Data.SqlClient NuGet package for .NET Core)

    0 comments No comments

  2. Karen Payne MVP 35,201 Reputation points
    2022-01-15T20:35:07.46+00:00

    There are two different packages where each have various versions which are installed via NuGet using manage NuGet packages or the command line in Visual Studio.

    https://www.nuget.org/packages/System.Data.SqlClient/

    https://www.nuget.org/packages/Microsoft.Data.SqlClient/

    Have no clue about filter its too wide of a topic.

    If you double click the project in Solution folder add the following and save. Visual Studio will install the package for you.

    <ItemGroup>
      <PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
    </ItemGroup>
    
    0 comments No comments

  3. Imac0818 6 Reputation points
    2022-01-16T21:56:29.36+00:00

    I have fixed the problem.
    There are dependencies but no references in solution explorer, right click on project node brings up csproj file
    which has <TargetFramework>net6.0-windows</TargetFramework>
    In the top level menu under project tab is add reference.
    Right click on project node has add\project references in the drop down menu. Has projects, shared projects, COM, Browse.
    I used Browse and tried to add a reference to C:\Windows\Microsoft.NET\assembly\GAC_64\System.Data\v4.0_4.0.0.0__b77a5c561934e089 but it was invalid
    .net 6.0 doesn't have System.Data.SqlClient. it is in .net platform extensions 6.
    Downloaded and ran MS compatability pack. Then ran nuget package manager and installed System.Data.SqlClient. Now it compiles with 0 errors,

    0 comments No comments