Linq-to-SQL datacontext not generating an object - asp.net

I am working in a .NET 2.0, recently upgraded to .NET 3.5 environment (VS2008, VB.NET) on an existing ASP.NET website project. I am able to generate a Linq-to-SQL Class (also called a DataContext?) in the App Code folder, drag over tables from an active connection, and save it. Let's call it MyDB. When I go to the code-behind file for my page and try to declare an object of "MyDBDataContext" it is not in the intellisense, indicating that it is not accessible.
I checked the references, and that has to be set correctly because I made the .dbml file.
I made a new test windows app project and it behaved exactly as expected, and I could follow this blog without a problem.
Is there something inherent to web projects that doesn't allow for these auto-generated objects to be usable? Is App Code the right place to declare it?
If you can't tell from the above, I am new to the industry and really new to LINQ.
thanks for your help.

Try expanding the dbml file and open the designer.cs file underneath, and make sure the DataContext class is in the same namespace as the codebehind class. If not, either change the namespace or include it with a using Namespace statement the top.

If it your dbml is in a folder inside of '/App_Code/' it may pick up the folder's name as a namespace. Eg: '/App_Code/DAL' would have the namespace 'DAL'. Give it a namespace in the designer or just use the namespace it is given, if this is the case.

Related

References and external class use problems

Using Visual Studio 2015 on an ASP.NET project. Everything works fine except now I have a block of code (A common AD utility function) that I want all the pages to be able to access. The original language (inherited project) is VB.NET, but it should work just fine either way.
I created a class file, named it CommonADFunctions.vb. The file uses System.DirectoryServices, and there's a reference in the project to that assembly. However, there are two problems:
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This may seem like a trivial ask, but what am I missing?
Turns out this was a simple property on the file itself I created a new Class File, and somehow the Build Action got set to Content rather than Compile. Setting this to Compile allowed the object to be referenced and instantiated properly. It doesn't fix the IDE intelliSense from APP_Code for DirectoryServices references in the code, but it compiles and runs, which is what I needed.
EDIT: Received external help which pointed this out.
Without code and screenshots to illustrate I can only recommend a few things to try.
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
The methods in the class have Private or Friend scope.
Its a static class (a Module in VB.Net) and you do not need to instantiate it.
You are somehow referencing an old DLL which doesn't have the methods. Check the reference to this AD project and make sure its "Referenced" via Project.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This sounds like a corrupt project file. Clean the solution, delete the .suo file, delete the obj & bin folders.
Then move the CommonADFunctions.vb to the App_Code folder and add the DLL reference to System.DirectoryServices. Or create the CommonADFunctions.vb file in App_Code to start with.

Copying/Moving App Code to New Project

Ok, I am in the process of breaking apart of intranet application (VS 2010 Web Site, ASP.NET Web Forms with VB code behind). During this process i'm trying to convert some of our our app_code files in to WCF rest service in a new project. However, when I copy or "add existing" vb files into the new "services" project. I get tons of errors including...
error BC30002: Type 'XXX' is not defined
warning BC40056: Namespace
or type specified in the Imports 'System.ServiceModel' doesn't
contain any public member or cannot be found. Make sure the
namespace or the type is defined and contains at least one public
member. Make sure the imported element name doesn't use any aliases.
From what i've read it may (or may not) have something to do with Assemblies and references that I just have very little knowledge on. I have added the namespaces from the current web.config to new project's web.config, and the files are identical. So there is something in the background that needs to be added to the new project I just don't know what its.
PLEASE HELP!!
THANKS
JOSH
UPDATE 1
So one of the errors i'm getting = "error BC30002: Type 'MailMessage' is not defined." Which is a namespace that is added to the web.config, which apparently isn't being picked up??? IDEAS?
I've come across a similar issue - not sure what the reasoning behind it is, but I got around it by creating a new .vb class with the same name as the one you want to copy, then copy and paste all the text from the old one to the new one. Save it, then it seems to like it - it didn't like me copying and pasting the actual file, nor did it like me adding existing files.
Hope this helps :/
This issue seems to be tied to the the differences between Web Application Projects versus Web Site Projects. Eventually I had to add different namespaces on the new project level.
Here is more info about WAP vs WSP....
http://msdn.microsoft.com/en-us/library/dd547590(v=vs.110).aspx

Errors when including Entity Data Model across referenced projects

Decided to take a dive into recreating a website so that it's an ASP.NET MVC web application. We were originally using the Entity Framework for it, figured to still use it since it works nicely enough. When doing some reading, noticed the there is no such thing as the App_Code/ folder for MVC applications, and that most people were creating a class library for their needs like that. So what I ended up doing was creating my MVC application and a class library, both in the same solution, and the MVC application was referencing the class library. The class library has my EDMX Entity Data Model file in it since some of my classes reference it and I thought that it would be easiest to have it in the library to reference. So here is a picture to what my solution layout is. But when I build and run the website, I get an error on a page that would reference the Entity Data Model. Here is a picture of what the error is.
So I thought, what if I placed the EDMX into my models folder since the error looks like it's complaining about not finding the entities. Here's a picture of my solution now with the move. But now, I'm getting a different error (picture). I'm kind of at a loss as to where to go from here. Is the way I have everything laid out and designed the proper and most efficient way to do it? If not could someone give me some advice?
Thanks, it's really appreciated!
Yes, you should be able to have the edmx in a class library and reference if from your main project. The first error is likely due to an incorrect connection string (check this blog post) but you may also have changed the build action of the edmx file somehow, make sure it is "Entity Deploy".
The second error is probably because you copied the edmx from your class library into the Models folder and it is still referencing the old namespace. To fix that, open the edmx in designer view and click on the background. One of the properties in the property window is Namespace, make sure that's set to the new place you want to keep it.
Edit:
Some things to try:
Clean and rebuild the project
Delete the Temporary ASP.NET Files folder referenced in the error message
Double check your cshtml files for old references to URG_MVC.Models namespace. For example, the line in the error should be IEnumerable<URGLibrary.Proposal>
Right click the edmx file and select Run Custom Tool to regenerate the model files

Subclassing Global and overriding Application_Start

I have a couple of web applications whose source code is missing. The project is compiled to a dll and is hosted on a IIS.
I have couple of questions to make.
What is the best way to recreate the
project from the dll file??
We are planning to change the
database server, and the database
connection strings are specified in
the Global.asax ( I mean the public
class Global : HttpApplication ). Is
there a way I can subclass this
Global and override the connection
strings? If yes, how can I make the
IIS refer to the new dll
Thank you all for any suggestions!!
For first part, use decompiler tools such as Reflector/ILSpy/dotPeek to convert IL code from DLL to higher level language such as C#. However, tools cannot get back comments, local variable names and project structure. You need to manually organize the code into files and project structures. From aspx files, you have to figure out the code-behind classes and then link up the source of the class into a correct named code-behind file - for example, if default.aspx says that it inherits from MyApp.Default then create file default.aspx.xs and put the source code for the class into that.
For second part, you can create a new class derived from Global and modify Global.asax to use that class - you need to put the assembly containing new class in bin folder and overwrite inherits clause in asax file to point to the type name of new class. You anyway need to inspect the code of your current Global class (using tools sighted above) to see if you can override connection strings by sub-classing.
Probably the best tool available to reverse engineer a dll into code is .NET Reflector. Unfortunately, the latest version is no longer free, but it is worth the money.
I am not sure exactly if this still applies if you can already reverse engineer your source code. However, I would recommend moving your connection strings outside of your project into web.config as a best practice. This way you can make the change in the future without changing any code.

Bulk Move ASP.NET Pages Into A New Namespace?

I have an C# ASP.NET Web Application Project where all the pages are in the global/default/top-level namespace. (I have no explicit namespace declarations. And when I look at my compiled web application's DLL in Red Gate's .NET Reflector, I can verify that all the classes are in the top-level .NET namespace.) Is there any good, automated way to move all the pages and custom controls into a new namespace, say "MyWebApplication"? Ideally it'd be nice to do it with just Visual Studio, but I'd be open to considering a commercial refactoring tool if necessary.
I thought maybe by setting the "Default namespace" property in the project's Application properties I could get the compiler to implicitly put all pages into the specified namespace, but this appears not to be the case; this "Default namespace" setting seems to make Visual Studio insert explicit namespace declarations into new pages, rather than implicitly affecting the namespace of any existing pages.
If it matters, my immediate motivation here is to try to run a static analysis tool (CAT.NET) on my web application; the tool seems to have a quirk or two with code in the global namespace.
Chris it may look silly as you are ready to pay for the refactoring tool to do it. But consider this:
The namespace is declared in the code-behind file by default as the project/application name. So if you project name is MyWeb the default namespace will be
namespace MyWeb
{
public partical class MyWebPage....
Now this is refered in ASPX page as follows:
<%Page ... CodeBehind="MyWebPage.aspx.cs" Inherits="MyWeb.MyWebPage"
You only need to change these two things to put into effect the namespace migration. And frankly this can be achieved using Find/Replace dialog. Please make a copy of your complete solution and try it!!
Cheers

Resources