ASP.NET + Linq to SQL - c#

I have a class in my App_Code directory that I'm trying to initialize from the root directory of my solution but it is not showing up in IntelliSense. I have tried changing the class's build action from Content to Compile but then IntelliSense throws a whole bunch of errors about not finding my DBML class.
Note DBML file, Class and aspx file are all in the same namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
^ Imports on class that I'm trying to initialize. (May be irrelevant)
Any pointers in the right direction would be appreciated.

You need to make sure that Build Action of none of the files in the App_Code folder is marked as Compile.
But this will bring its own side effects that intellisense may not
work very well for these files inside VS as they will not be treated
as Class files by VS… But the key point is that you do not really need
“App_Code in Web Application Projects (WAP) if you do not intend to
put random code files or modify existing code files in App_Code folder
directly on your production server…
http://vishaljoshi.blogspot.fi/2009/07/appcode-folder-doesnt-work-with-web.html

Related

I'm getting a CS0234 "The type or namespace name "Services" does not exist in the namespace" even though I can see it in file structure

I was following a tutorial from Microsoft found at https://learn.microsoft.com/en-us/training/modules/create-razor-pages-aspnet-core/
At one point it instructs to add a folder under the root project called "Services". Within that folder is a file which needs to be accessed. Upon creating the project, another folder called "Models" was automatically created under the root folder.
I include the two lines at the top of another file:
using RazorPagesDoughnuts.Services; using RazorPagesDoughnuts.Models;
The Models statement works no problem. The Services statement generates the error. I have searched many resources and cannot find a solution.
I am using vscode 1.71.2 and .net 6.0
Screenshot of file structure and statements
The namespace provided in the file DoughnutService.cs is probably not correct. Change that to RazorPagesDoughnuts.Services and check.
In C# using statements are not based on file structure, but rather on namespaces. If there aren't any files with the statement namespace RazorPagesDoughnuts.Services in the project, then you will not be able to reference it in other files.
Make sure that DoughnutService.cs contains this namespace statement.

How to use a "flat" namespace in whole C# project

I want to use the same namespace for my whole c# class library. But the default behavior for new files is to use a namespace like [Base namespace].[Folder 1].[Folder 2].
Is there a simple way or plugin for visual studio 2019 to make all new files use the same namespace? So just [Base namespace] whatever the folder?
But just for a single project, I don't want to change the global file templates.
Although all concerns mentioned in comments, there's simple way to achieve this. Just go to project properties and define default namespace:
Now every added class has this namespace defined:
using System;
using System.Collections.Generic;
using System.Text;
namespace MyGlobalNamespae
{
class Class1
{
}
}
I have found a workaround I'm pleased with:
I installed this nice extension Visual Studio Namespace Fixer. It allows you to define a template for your namespaces and then can apply it to all .cs, .xaml, and other files in your project.
Under Options -> Namespace Fixer options -> Use default project namespace I defined the format {projectRootNamespace} for Namespace format.
Not I just have to select my files and folders in the projects base folder (in Visual Studio, not in the windows explorer), rightclick and select Adjust namespaces and all files in all subfolders will change their namespace to the projects root namespace :-)
Awesome if you create and use a lot of class and other files in your NuGet lib.

C# compiler gets Environment namespace wrong after I add a new dll

I have an existing C# file, A.cs, which uses Environment.GetEnvironmentVariable() in System. I have using System; in the beginning of the file. It compiles fine.
And in another file, B.cs, in the same project, I need to add using AnotherNamespace.Environment.APackage. So I need to add a new reference dll to my project.
B.cs compiles fine. but A.cs has compiler error saying GetEnvironmentVariable() does not exist in AnotherNamespace.Environment.
I would like to know why the compiler thinks that Environment is from AnotherNamespace.Environment instead of System? I did not change A.cs file at all.
If you need to avoid namespace collisions, you either need to explicitly specify usage, as per
System.Environment.GetEnvironmentVariable()
Or a using alias, as per
using Environment = System.Environment;
However, either of these should only be necessary if A.cs contains using AnotherNamespace. This error can also occur if the malfucntioning code is contained with AnotherNamespace, including being within a nested namespace such as AnotherNamespace.Nest.Something.

Type or namespace could not be found from App_code folder

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.

Type or namespace could not be found (Web Site)

I created an ASP.NET Web App project and it builds and works correctly now. However, I am trying to add this project to my website. So what I've done is I create a new Web Site from VS and then I added all of the .CS files from my Web App project to this Web Site.
Even though the Web App project builds successfully, when I use this same code in my Web Site and I build it, I get the error:
The type or namespacae name
'ADONET_namespace' could not be found
(are you missing a user directive or
an assembly reference?)
Here is the top part of my master.cs file:
using System;
using System.Collections.Generic;
using System.Collections;
namespace AddFileToSQL
{
public partial class _Default : System.Web.UI.Page
{
And the top of my ADONET_methods.cs file:
using System;
using System.Collections;
using System.Data;
namespace ADONET_namespace
{
public class ADONET_methods : System.Web.UI.WebControls
{
Finally the top of my child class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ADONET_namespace;
namespace AddFileToSQL
{
public partial class DataMatch : _Default
{
In this child class file, there is a squiggly line under ADONET_namespace and hovering over it returns the error specified above. I have this ADONET_methods.cs file stored in same file structure format on this Web Site as I do on my Web App project. So if it builds successfully in that project, why not in this Web Site?
I have searched Stack Overflow for similar questions and I found one question which was asking the same thing as me, but I tried all of the solutions listed there. I also googled this problem and tried all of the suggestions, but none of them worked either. And I have added this ADONET_methods.cs file to different locations within this Web Site to test out if it was being found or not, like under App_Data folder, References folder, and also in the same level as the other .CS files (like it is in the Web App project).
What else can I try? This code is written in C# by the way in VS 2008, running on an XP Pro with IIS6 Manager.
For each code -- example: .cs -- file in the App_Code folder, ensure that the Build Action property is set to "Compile" and not "Content" or anything else.
Try putting it in your App_Code directory.
Not sure if this is causing any issues, but do you want your class in the ADONET_methods.cs file to be named ADONET_namespace? It seems odd to have both the class and the namespace with the same name.

Categories