EGN
EGN

Reputation: 2572

CSharpAddImportCodeFixProvider encountered an error and has been disabled

I had my PC re-imaged for me. I have Visual Studio Version 14.0.25123.00 Update 2 installed on my computer. I'm getting this error when I try to use VS intellisense to reference another project.

CSharpAddImportCodeFixProvider encountered an error and has been disabled

I have two projects. One of them using namespace ProjectName.Web. And the other project using ProjectName.Web.Controllers. The provider crashes when I reference ProjectName.Web.Controllers, I believe because it is setup as a project. Any idea how to fix this?

Upvotes: 23

Views: 15874

Answers (7)

Mohammad
Mohammad

Reputation: 11

Had the same problem solved by installing the sqlClient package from nuget package manager right-click on the project choose manage nuget packages.. and go to browse tab and search for System.Data.SqlClient and install it. that easy :)

Upvotes: 1

Sergio Villalobos
Sergio Villalobos

Reputation: 73

I end up with this same error. what i did was to manually go and find the nuget packet for System.Data.SqlClient, installed and then invoke it in the class i was working on, like:

using System.Data.SqlClient;

Dont know if this is some kind of bug, cause it happens on a new blank project i create using visual studio 2019 community

hope it helps someone

Upvotes: 1

Mladen Janjic
Mladen Janjic

Reputation: 117

Happened to me when chose Add using System.Data.SqlClient automatically after typing using (SqlConnection...){} in DataLayer, in one of Repository classes.

Nothing helped (except creating new project), but I saw that the problem was with loading System.Data.SqlClient.dll file, although it existed in appropriate folder.

Found by trial and error that after removing Dependencies->Assemblies->System.Data.SqlClient from DataLayer (right click -> Remove, or just press Delete key when selected), I can Add System.Data.SqlClient without any errors.

Didn't try for other cases where CSharpAddImportCodeFixProvider encountered an error and has been disabled message appears, but the solution might be similar.

In my case the problem appeared probably due to .net Core version conflict or something like that, because the project was on external drive and created on another computer.

Edit: Also, some errors might be caused by .vs folder (hidden by default) inside Solution folder, especially if the project is moved between different computers. I know from experience that IntelliSense can seem to be broken and classes from other namespace would be unavailable although using namespace_name statement is present. The solution is to delete .vs folder or just avoid copying it with the project, as suggested here: https://weblog.west-wind.com/posts/2018/Aug/07/Fixing-Visual-Studio-Intellisense-Errors

Upvotes: 2

Aicilec
Aicilec

Reputation: 11

anyone trying this solution from here ?

https://developercommunity.visualstudio.com/content/problem/623872/add-import-not-working.html

the last reply solves my issue..

Tools > Options > Text Editor > C# > Advanced

turning off Suggest usings for types in NuGet packages

Upvotes: 1

user4864425
user4864425

Reputation:

There may be multiple reasons why this error occurs. So this answer may not apply to all situations, though it seems that it only occurs when another project is referenced.

The error does not occur for all statements. In my case I had the following code:

private System.Threading.Timer Timer;

public void Start()
{
    Timer.Change(0, 60000);
}

As soon as intellisense would open for Timer.Change( the error occurred. Please note that I had no parameters at that point. If valid parameters are present there will be no error.

I could solve the issue by updating the version of the framework. I found out that both projects targetted different frameworks, resp. 4.5.2 and 4.6.

As long as the framework versions are different the error occurs. As soon as both are equal (either 4.5.2 or 4.6) the error no longer shows.

I have tested this with VisualStudioVersion = 14.0.25420.1 (Visual Studio Community 2015).

-- update --

I've reported this as a bug to Microsoft. Including steps to reproduce.

Upvotes: 1

ShieldOfSalvation
ShieldOfSalvation

Reputation: 1351

Judging from the label you were given, "CSharpAddImportCodeFixProvider", I'd guess your problem was due to Visual Studio trying to identify and/or correct a missing "using" statement at the top of your C# source code file in which you made reference to a class that needed it. The Visual Studio components that usually deal with this type of problem are Intellisense, or third-party syntax highlighting/correction plugins like JetBrains' ReSharper.

On second thought, I'm not quite sure it's Intellisense's fault as opposed to the plugin ReSharper's. That's to be determined.

I did file a similar bug report with Microsoft. The error in my case seemed to be a result of the Intellisense not knowing how to deal with a logic error in my own code (see https://connect.microsoft.com/VisualStudio/feedback/details/3133049).

In my case, I had inadvertently placed code for a method outside its class definition, though inside its similarly named namespace. Visual Studio 2015 Update 3 complained,

'GenerateVariableCodeFixProvider' encountered an error and has been disabled.

The fix was to move my method back into its corresponding class definition, but it definitely brought a Visual Studio bug up to the surface.

Specifically, Visual Studio Intellisense had seen the line of code,

    Response.Write("I did something"); 

placed in a method that was declared outside a class definition (i.e., inside a namespace, but inadvertently not inside its class). The "'FeatureLabel' encountered an error and has been disabled" error was then displayed in a yellow bar across the top of my editor window and an "Enable" button and an "Enable and ignore future errors" button were displayed next to it.

I believe that Intellisense (or ReSharper?) tried to automatically deal with the situation and attempted to generate a variable for the keyword, "Response", but it tripped trying to do so--which in turn caused the error that was displayed.

Upvotes: 0

Auronmatrix
Auronmatrix

Reputation: 81

I had the same issue on VS-2015 update 3.
I did was :

1) Closing visual studio

2) restarting as administrator

Upvotes: 5

Related Questions