namespace or class could not be found (ASP.NET WebSite "project") - c#

I am currently trying to cleanup a bit of a corporate website that I inherited here. I managed to clean up most of the errors in the website but something is still up here.
I have one masterpage that have this code :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage {
public lists m_listsClass = new lists();
(no it's not a typo the S in lists).
Now in App_code I have one class lists.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
/// Summary description for lists
/// </summary>
public class lists
{
public lists()
{
When I try to build the website in visual studio 2008 I have this error :
Error 3 The type or namespace name 'lists' could not be found (are you missing a using directive or an assembly reference?) C:\Users\egirard\Documents\Visual Studio 2008\Projects\iFuzioncorp\iFuzioncorp\Masters\MasterPage.master.cs 23 12 iFuzioncorp
Am I missing something ?
Also I saw some strange behaviour on the server. Apparently according to IIS7 it is compiling using .net 2.0 (the pool is configured with .net 2) but there are some "using" statements that include Linq ... how is it possible to compile the page if Linq is not part of the .net 2 itself ?
Just edited with news details on the code itself. No namespace at all everywhere.

Hi There – i had a similar problem; all my namespaces and inheritance was in place. Then i then noticed that the class file’s build action was set to “Content” not “Compile” (in the properties window.

For whatever worth might there be for an answer (possibly not the right one) after many months, i think i should contribute this:
There is a case that this happens, when you place a web site inside another (ie in a subfolder).
In that case, the only App_Code folder that is legitimate is the App_code folder of the outer web site. That is, the App_Code folder right under the root of the master web site.
Maybe (say maybe) there should be no need to transform your web site to a web application, if you place the class file inside the App_code folder of the ROOT web site.

include the namespace under which lists calss is defined
or
define both the master page and lists class under the same namespace

Finally I understood quite lately that it was a website and not a web application I had to question the guys here to get it... So it's quite normal all the error I had. I haven't had the occasion to convert it first.

Make sure that in your masterpage, you have an #include statement for the namespace that the lists class is a part of (if they're in seperate namespaces, the masterpage isn't going to automatically pick up on it).
As for the strange server side behavior, .NET 2.0, 3.0, and 3.5 all run inside of .NET 2.0 app pools in IIS. It looks strange at first, but you get used to it. Here's a link with a little more in-depth explination:
The Way I See It: Where is ASP.NET 3.5 on IIS?

In order to use a type, you must reference the assembly which defines it and include the appropriate namespace.
Using only includes namespaces, if you don't use any types from said namespaces it has no effect.

is lists.cs wrapped in a namespace? if it is the case then you need to add the namespace (yournamespace.lists) or include it in masterpage. Check Also if your MasterPage is in a Namespace

Modify the Build Action of the App_Code Class to Compile.
Clean and Build the Project
This worked for me.

I had this problem myself
It wasn't working because the project I was referencing was set as a console application in its properties instead of a class library

Related

RootNamespace not being used as namespace prefix

I recently created a new .net6 project and moved my source code into it.
I've set the following in the project file
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
In the project properties there is a section at the bottom of Application -> General there is a "Default namespace" which also has "$(MSBuildProjectName.Replace(" ", "_"))"
For the sake of this question I have omitted the solution completely and let's assume my project structure is as follows
MyProject (this is a project with project file MyProject.csproj)
DB (this is a folder in the project)
MySQL (this is a sub-folder under the DB folder)
MSSQL (this is a sub-folder under the DB folder)
UI (this is a folder in the project)
If I add a new class at the root level of my project it's namespace is MyProject however if I add a new class to DB it's namespace is DB.
I expect this to be MyProject.DB
Similarly if I add a new class to the MSSQL project I get a namespace of
DB.MSSQL whereas I expect this to be MyProject.DB.MSSQL
I have tried hard coding the $(MSBuildProjectName.Replace(" ", "_")) as MyProject but this doesn't help.
As strange as this sounds, the problem was with WebView2 version 1.0.1343.22
Updating to version 1.0.1370.28 resolved the issue
I did also recreate this in a fresh solution with only 1 project and no other dependencies to ensure that is the case.
Interestingly the default using statements are also different.
With version 1.0.1343.22 installed, creating a new class file you get
using System;
using System.Collections.Generic;
using System.Text;
namespace DB.MsSQL
{
internal class Class1
{
}
}
Whereas with version 1.0.1370.28 a new class file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebView2NamespaceIssue.DB.MsSQL
{
internal class Class2
{
}
}
Both of these classes were created in the same folder of the same project.
The only difference was changing the version of WebView2 being referenced

Using AssembliesFromPath from StructureMap throws MissingMethodException

I am trying to scan all assemblies in a subfolder of my main project and then add the contained registry to my main registry in order to override default registry entries, where necessary and intercept types for dependency injection.
For this I create the following registry
public PluginRegistryAdder(string pluginPath)
{
Scan(x =>
{
x.AssembliesFromPath(pluginPath);
x.LookForRegistries();
});
}
which I plan to add to my main container like this:
var pluginRegistries = new PluginRegistryAdder(pluginPath);
Container.Configure(_ => _.IncludeRegistry(pluginRegistries));
The problem I am now facing is that I get this error, when using AssembliesFromPath and I don't know how to fix it:
System.MissingMethodException: Method not found: 'Void StructureMap.Graph.IAssemblyScanner.AssembliesFromPath(System.String)'.
Googling suggests that I should use the namespace StructureMap.Graph, but this did not solve the problem. Here are my using statements, of which only StructureMap is not greyed out (e.g. marked as not redundant) by Visual Studio (I was trying to find the missing reference, but nothing helped):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StructureMap;
using StructureMap.Graph;
using StructureMap.Configuration.DSL;
using StructureMap.Configuration;
using StructureMap.Graph.Scanning;
using StructureMap.Util;
Any suggestions, what I should try?!
UPDATES:
Note that when I comment out just the line with AssembliesFromPath, solution runs just fine, although LookForRegistries is definded in the same class as AssembliesFromPath.
AssembliesFromApplicationBaseDirectory gives me the same problem.
This problem seems to have been caused by a version mismatch between my DI project and my main project. After some trying I ended up reinstalling the newest version of StructureMap in both projects and the problem went away.

C# using System.Xaml missing assembly reference

I'm writing a C# Console Application that is targeted towards .Net 4.5. I want to use Xaml Services to save and read a List data structure to file. I'm using VS 2013 Pro.
The .net doc's say Xaml has been in .NET since 4.0(?) I have my projected targeted to 4.5, but even with 4.0, 4.5.1, 4.6, and 4.6.1... same missing assembly reference. I'm doing a
using System;
using System.Collections.Generic;
using System.IO;
using System.Xaml; // <-- this is getting the assembly error
using System.Xml;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
But that is where the missing reference is getting the error...
I've used it before in Win Forms. It is that maybe it's excluded for console applications? Or am I missing something that Xaml Class depends on before using that assembly?
Could it be that you did not add a project reference to the assembly "System.Xaml.dll"? The Xaml functionality is not contained in the assemblies which are included in a new VS console project by default. (Right-Click on the "References" entry of the project, then select "Add References", then browse to "Framework" and look for System.Xaml, which refers to the dll of that name).
A namespace however does not necessarily correspond uniquely to an assembly, so you might require even more assemblies. If you know which types you need, you can browse the MSDN documentation for looking which assembly might still be required.

Regarding adding namespace in common area

we always add namespace at the top of the every form in win apps
like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Business;
using System.Data.SqlClient;
using Business;
and most of namespace are common in most form but in asp.net there is a provision to add namespace only once in web.config file. so we dont have to add it in all web form and it save time. so i just need to know is there any same sort of provision for our win apps. how to achieve it. thanks
No, there isn't any sort of provision for your WinForm applications as there is for ASP.NET applications using the <namespaces> section in web.config. Also note that this applies only to .aspx pages and not code behind C#. In C# you have to add proper using statements in order to bring the types you want to use into scope.
One method is to add the standard using directives into the common templates.
You don't mention which version of visual studio you are using but in VS2010 you can find the templates in:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Change the Class.cs file in the zip to include the usings you want by default.
The only downside to this is that it is per machine - so you will need to do it on each developers computer - although I daresay you could roll it out with Active Directory (that's a question for serverfault.com).
I believe it also possible to set up team templates - but it's not something I have tried. More information available here:
http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.

Categories

Resources