Visual Studio Does not recognize System.Linq - c#

I created this program and C# and it worked correctly a few weeks ago.
Now, I copied all of this code to a another project I am working on. I added it to a brand new C# Class inside of the project I am working on. Visual Studio didn't recognize the assembly references that say "not found" in the comments.
This is just bizarre since they were found in the previous program. Does anyone know why Visual Studio can't find these assembly references.
using System;
using System.Collections.Generic;
using System.Linq; //not found
using System.Text;
using XML_Creator; //not found
using System.Xml.Linq; //not found
using System.IO;

An educated guess: It's probably the .NET framework target version of the new project.
System.Linq was introduced on .NET Framework 3.5.
Go to Project -> Properties -> Application and check the Target Framework property. It's probably a target framework prior to 3.5

You need to add refrence to system.Core.

Step 1) Go to your Project Solution Explorer and Select
References
Step 2) Right click it on References and Select Add Reference... a form will appear
Step 3) Search linq on Search bar
Step 4) Now Select/tick the System.XML.Linq checkbox
Step 5) Click Ok

If your file is in App_Code folder, try moving it to another folder. For me its solved the problem.

You need to right click on the solution in Solution Explorer and add a reference to System.Xml.Linq and/or System.Linq in Project Properties.
This has hit me several times - I see it a lot with System.Configuration as well. (For some reason those two references seem to be particularly picky)

For me I tried this and worked:
PROJECT==>Add Reference ==> Browse ==>
C:\Windows\winsxs\x86_netfx35linq-system.core_31bf3856ad364e35_6.1.7601.17514_none_6161fc35ed136622\System.core.dll
Or
you can simply find the System.core.dll using windows search on Os System Drive

I had a similar issue. I upgraded my asp.net project to .NET Framework 4.5.x and it worked. I did tried 4.5.1 first and then later 4.5.0 and both seem to be helping.

Related

References c# visual Studio

I have been working on this project in Visual Studio 2010 using C# a few days before Windows 10 update and it worked fine but now when I try to run the program and it tells me the following :
The type or name of the 'Drawing' namespace does not exist in the
'Tekla.Structures' namespace (missing an assembly reference?)
and the same for Tekla.Structures.Model, Tekla.Structures.Model.Operations ... as you can see in the following image:
What I am doing is removing the references and adding them again and in this way the program recognizes it well, but if I clean the solution and try to generate the solution again, the problem arises again ...
What could be the problem?
It seems that you want to use Tekla reference correctly.
I suggest that you can use NugetPackage.
First, right click->References->choose Manage Nuget packages.
Second, choose the Browse->input 'TeklaOpenAPI' in textbox and choose the first.
Third, click install.
Finally after it has been installed, you will see the following correct using code without warning.
first you remove tekla references, select and right click, then remove
remove reference
after right click on References and add reference
add references
after you need add dll corresponding the version you want execute
select references
pay attention when building the app, it only runs in the version you chose the dlls.

I can't see a referenced class of another namespace in C#

I have 2 projects:
ConstrainedScheduleInterfaces
ConstrainedSchedule that has a folder (Testing) with my class that need the reference, here's the code:
Tests.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using
using NUnit.Framework;
using Ninject;
using ConstrainedScheduleInterfaces;
namespace ConstrainedSchedule.Testing
{
internal static class Configuration
{
...........
}
}
I added the reference to the ConstrainedSchedule project, but the using ConstrainedScheduleInterfaces; is marked red as not found.
Both the project has destination framework set .NET Framework 4.5
Any help? Thanks
Does the project contain a reference to the other project? You can't just add the namespace, the project itself needs an assembly reference to the other project which has that namespace.
In Visual Studio, open the Solution Explorer. Right-click on the ConstrainedSchedule project and select something along the lines of "Add Reference." In the dialog, select project references (depending on the version of Visual Studio it may be a tab or some other interface indicating projects as part of the solution). Select the ConstrainedScheduleInterfaces project and add the reference.
More information here.
For other people who have this problem, who have already added the reference to the dll and have made sure you have the right namespace at the top, I've found another thing that can cause this.
Select-click the file in visual studio and choose properties. In the Advanced part of properties, look for Build Action. Change it to compile if it's not already on that, and try to build again.
There might be another reason: different target frameworks.
My main project had ".NET Framework 4.7.2" and the referenced project (linked from another solution) had ".NET Framework 4.5.1".
Select the project properties and change the target framework.
I was having this issue after renaming a project and namespace due to a naming conflict.
Basically my main project had a reference to a project I made called Common, but then I needed to reference another project called Common that another dev had written. After renaming my project something like SpecificCommon, I was able to have references to both Common and SpecificCommon in my main project. However, all references to SpecificCommon namespace were now broken.
At first I thought this was some problem with the namespace refactor, but it turns out that it was because SpecificCommon and Common both still had the same assembly name (Common.dll). Changing the assembly name in SpecificCommon's properties and rebuilding solved the issue for me.

Can't use System.Windows.Forms

I have tried making (my first) a C# program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
Console.ReadLine();
}
}
}
This goes well, but if I try using System.Windows.Forms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
System.MessageBox("hello");
Console.ReadLine();
}
}
}
This is the error I get:
Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\Ramy\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 5 14 ConsoleApplication1
Some details:
- I am using Visual Studio 2012;
- I have installed the .NET Development Kit;
- It is a Console Application.
Maybe it's because on a Console Application can't use System.Windows.Forms?
If so, what program should be? I also have tried with a form, but I was only displaying a window and no code.
A console application does not automatically add a reference to System.Windows.Forms.dll.
Right-click your project in Solution Explorer and select Add reference... and then find System.Windows.Forms and add it.
For Those using Visual Studio 2022 with .Net Core 6.0
Sorry to revive this thread, but I created an account just to do so, as none of the solutions I found searching google for days worked for me alone, and seemed to only bring up only outdated tutorials.
Not Working =(
What DID work for me
Double click your project (opening the csproj editor window)
Add the following lines (replacing the existing TargetFramework line):
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
My window, for referrence, looks like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Note Both lines! I read several solutions / videos that stopped simply after adding the "UseWindowsForms" tag, which did not solve the problem for me, even after unloading and reloading/closing and opening etc.
I stumbled on this solution after applying the "UseWindowsForms" tag, and then in desperation changing my NET framework in properties to 5.0, which caused it to work, and then noted when changing back to 6.0 it still worked.
If you forget the Framework tag in the future, you can reproduce the effect just by flipping your properties back and forth...
Right click the project, and go to properties.
Change the Target framework from .NET 6.0...to....NET 5.0
Exit back to your code. Which after a few moments will show the System.Windows.Forms connecting properly.
Open back up properties.
Change the Target framework from .Net 5.0, back to .Net 6.0
Congratulations
You have (or at least I have) a .Net 6.0 project that is properly allowing me to use System.Windows.Forms (Including the Clipboard, which I suspect many here are looking for...).
I did due diligence on this, testing it multiple times across multiple projects, and it (at least for my setup) consistently works!
BTW. For those wondering. the [STAThread] Attribute tag seen in the pictures is needed to allow the Clipboard class to function. (this is also why I am not using top level statements in the example, but if you don't need that Class, the example works with top level statements (I needed it to show my Clipboard test....)
You have to add the reference of the namespace : System.Windows.Forms to your project, because for some reason it is not already added, so you can add New Reference from Visual Studio menu.
Right click on "Reference" ▶ "Add New Reference" ▶ "System.Windows.Forms"
Adding System.Windows.Forms reference requires .NET Framework project type:
I was using .NET Core project type. This project type doesn't allow us to add assemblies into its project references. I had to move to .NET Framework project type before adding System.Windows.Forms assembly to my references as described in Kendall Frey answer.
Note: There is reference System_Windows_Forms available under COM tab (for both .NET Core and .NET Framework). It is not the right one. It has to be System.Windows.Forms under Assemblies tab.
To add the reference to "System.Windows.Forms", it seems to be a little different for Visual Studio Community 2017.
1) Go to solution explorer and select references
2) Right-click and select Add references
3) In Assemblies, check System.Windows.Forms and press ok
4) That's it.
If you're using Visual Studio 2022 open the Project Properties and check the Windows.Forms - Enable Windows Forms for this Project setting.
Ensure Solution Explorer is visible
In MS Studio 2008 Go to view and click Solution explorer
In Solution explorer go to Reference
Right click on Reference and select Add Reference..
Select .NET tab
Scroll down till you find System.Drawing -> select it -> click on OK button
Do the same for System.Windows.Forms
When you run your form this will work
(eddie lives somewhere in time)
go to the side project panel, right click on references -> add reference and find System.Windows.Forms
Any time some error like this occurs (some namespace you added is missing that is obviously there) the solution is probably this - adding a reference.
This is needed because your default project does not include everything because you probably wont need it so it saves space. A good practice is to exclude things you're not using.
may be necesssary,
unreference system.windows.forms
and reference again.
For some reason, none of the solutions worked for me (Windows 11, VS 2022). I actually needed to use the "Windows Form app" template. See: https://learn.microsoft.com/en-us/visualstudio/ide/create-csharp-winform-visual-studio?view=vs-2022.
just add reference to System.Windows.Forms.dll

Namespace 'SharePoint' does not exist in the namespace 'Microsoft'

So I am starting to learn C#, like literally just started learning, and coming from a Java background, it doesn't look too bad. However, I have a question. I am following THIS tutorial on using the client-object model. And just starting from the top, I added the references, but using Microsoft.SharePoint.Client; keeps giving me the error that "the namespace 'SharePoint' does not exist in the namespace 'Microsoft', but I clearly see it on the right side panel. So looking at the instructions, the only difference I can think of is that fact that I am using Visual Studio Express and thus do not have the option to choose which framework to use when creating a new project. Other than that, I don't know what the problem might be. Does anyone have any ideas on what else I could be missing or how to correct this problem?
Make sure that the target framework is 3.5 and not 4 i.e for SP2010
Did you add the references to the Microsoft.SharePoint.Client assembly and Microsoft.SharePoint.Client.Runtime assembly as noted near the beginning of that tutorial?
Add required references to the solution.
Make sure that the target framework is 4 for SP2013(3.5 for SP2010).
Did you do this part of the tutorial you mentioned above?
To build the application, you must add references to two assemblies,
Microsoft.SharePoint.Client.dll and
Microsoft.SharePoint.Client.Runtime.dll. Installing SharePoint
Foundation installs these assemblies on the server. The two assemblies
are located in the following directory:
%ProgramFiles%\Common Files\Microsoft Shared\web server
extensions\14\ISAPI
Take a look at the references in your project and make sure you have the reference to the assembly. If it is not there try adding it, right click -->add reference and find "Microsoft.SharePoint.Client"
Thanks to those who mentioned the 4.0 framework.
Mine defaulted to .NET Framework 4 Client Profile (and I have no idea what that means), and the Namespaces looked good in Intellisense, but the build would say they weren't found! Crazy.
for anyone developing for SP2019, you need to target .net 4.5

I can't add a using reference on my C# application

I'm using Visual Studio 2010 Beta2 and I can't add a reference to:
system.drawing
I've tried right click on the solution explorer but I can't find it there.
What gives?
Here is all you need to do in VS 2010:
In Solution Explorer right click on the References folder of your project
Select Add Reference.
Under the Assemblies category there is a sub category of Framework. If that is selected you should see a list of all of the Microsoft .NET Framework libraries
Find System.Drawing and double click it.
Then your project will have the reference added to it!
In VS2010 Beta 2 the references list may not be automatically sorted because it loads asynchronously. You may need to wait for it to finish loading then click the column header to sort.
Solution explorer is for adding references to external libraries.
It sounds like you just want to add the line
using System.Drawing;
to the top of one particular file.

Categories