Visual Studio thinks my controls are missing, but they're not. "The name xx does not exist in the current context" - asp.net

When working with an ASP.NET application in Visual Studio 2008, my errors window often contains a list of errors such as;
The name 'txtUsername' does not exists in the current context.
When I double click on the error, it will go to the source, show the control underlined in red, then realise it's mistake and remove those errors. It's a real pain because if you have a genuine error, you have go and hunt for it.

Have you tried building the site?
Build -> Build Web Site

Related

VS 2013 templates

All the msdn documents say I should have the following template options (see image below) when trying to create a new asp.net application but for some reason the only options I have are Empty, Web Forms, and Azure Mobile Service.
Does anyone know how I might go about restoring the missing options here as I need to create a new MVC application?
EDIT: Further clarification:
When following the steps carried out here ...
http://www.asp.net/visual-studio/overview/2013/creating-web-projects-in-visual-studio
... when I get to the above dialog I do not have any mention at all of both web API and MVC.
Finally cracked it ...
For some reason the web developer tools were not installed as part of my installation (likely due to the repair that happened last week).
So if anyone else gets this the fix is as follows:
Go to control panel > programs and features
Find "Visual Studio Ultimate 2013" entry.
Right click > change.
Click on "modify"
Tick the box labelled "Microsoft Web Developer Tools" and click to continue.
After running through the setup process everything should now be in place !
Wierd ... these options were in their correct place but apparently not after the repair !
Please ..Select WebForms-> Press Below Check for MVC (This optional u can leave it for now )-> Then you wil have an option for
MVC Application
WEB API application
ASP.NET WEB Application and so on!!
If no
reinstall or run some update for VS2013 ... its corrupted installation i guess or
clear APP-DATA ,TEMP AND MYDOCUEMNTS for VS related stuff...if u have massed it while working other settings...
Cheers!

activity could not be loaded because of errors in the XAML

Considering the following scenario:
Create MyCodeActivity1 with argument MyArgument1
Add this activity to a myworkflow.xaml
Rename MyArgument1 in MyCodeActivity1 to MyRenamedArgument1
If you open myworkflow.xaml, visual studio will show this error:
activity could not be loaded because of errors in the XAML
Is there a facility where visual studio can give more information about the error please?
For now I have created a small windows application that tries to execute the workflow and shows an error message if invalid. This does give a clear message. But it would be great if visual studio provided this functionality directly.
No.
Nope.
Nuh uh.
Visual Studio's refactoring can't handle change in Activity code => change in XamlAppDef XML. Its not surprising, such a chain of logic would be complex to achieve. So whenever you do this you must expect your workflows will have to be fixed. Either plan ahead, or...
Any time a workflow won't compile, or if it shows the red box of pain in the design surface, you should always right click on the workflow file, select Open With... and select the XML (text) editor. You will see the squiggly error lines and more detail about the error in this view.
I got the same error("activity could not be loaded because of errors in the XAML"), But in my case, I had created a Code Activity and used that in a State Machine. But, I found out that the defined Code Activity had not been shown in the Toolbox. It means that the Code Activity is not identifiable to Windows Work Flow.

What is the process involved in viewing a webservice in a browser from within visual studio?

I have created a new VS2008 ASP.Net Web service project, with the default name WebService1. If I right click on the Service1.asmx file and select 'View in Browser' what are the processes that go on to make this happen? I am asking because I have a situation where when I run this from a visual studio project started in our development shell (which sets up a common build environment) I cannot get the web service to show up in the browser.
It starts the asp.net development server and creates a single file:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\c43ddc22\268ae91b\hash\hash.web
but when I start it from a stand alone project i get a whole slew of files in here:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\edad4eee\d198cf0e\App_Web_defaultwsdlhelpgenerator.aspx.cdcab7d2.vicgkf94.dll
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\edad4eee\d198cf0e\service1.asmx.cdcab7d2.compiled
etc etc
I am trying to debug this but not really getting anywhere. i have inspected the output from VS but the only option I get is for the build output, which is basic and doesn't really contain any information that is useful. I have tried running both versions with DebugView running but no output there either.
I would like to know if there are any log files I could look at, or if anyone has any suggestions on how I might be able to debug what is going wrong here?
For completeness the output I get when it doesn't work is:
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not create type 'WebService1.Service1'.
Source Error:
Line 1:
Source File: /Service1.asmx Line: 1
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082
Ok, not sure that its perfect but I found a resolution to this issue. the problems seems to be that the development shell redirects the <OutputPath> of the projects visual studio is building, and this was causing problems as, although the dependencies of the web service project were being copied into the $(ProjectDir)Bin\ directory, the actual webservice dll itself was not. The solution (workaround?) seems to be to add this to the post build event command line:
xcopy /y "$(TargetPath)" "$(ProjectDir)Bin\"
Then i am able to right click on the .asmx file and select view in browser and everything works as expected.
Hope this might help someone else in the future. or maybe even me in another place and time.

ASP.NET WebControls are not appearing in my VS 2008 toolbox

It's been a while since I've created a new ASP.NET web application. (I've been doing ASP.NET MVC for the last 9 months, and hadn't done an ASP.NET site for about a year before that.) Now I've created a new regular ASP.NET app and when I look at the Default.aspx file, Visual Studio is only displaying the HTML tab in the Toolbox. I don't have the WebControls or the data access controls. (I don't even remember what other tabs I should be seeing.)
When I try to add a web control manually like this,
<asp:Label id="LabelTest" runat="server"></asp:Label>
I get a green squiggly and the tool tip says Unrecognized namespace 'asp'.
What do I need to do to get the Web Controls in my toolbox and also get the namespace to be recognized?
I've got this line in my page load event:
LabelTest.Text = "testing"
My app compiles and runs as expected -- the label displays testing.
So, I could word around it, but this will just make designing the forms more difficult.
Update: I've tried all 3 of the suggestions posted as answers so far and nothing has helped. Now I'm seeing a different error in addition to what I was getting before. In the designer I now see this:
Error Creating Control - LabelTest
Could not load file or assembly 'Microsoft.Web.Authoring' or one of its dependencies. The system cannot find the file specified.
Have you tried repairing the Visual Studio installation?
Pops-in the disc and select "Repair"?
If that doesn't work, then I think you should just re-install Visual Studio... It seems like a plugins/add-on corrupted the toolbox
Have you tried
removing and then re-adding the assembly references and then
rebuilding the solution?
Have you tried the "Reset Toolbox" command?
reset toolbox http://chakrit.net/files/stackoverflow/so_reset_toolbox.png
...

How to convert ASP.NET website to ASP.NET web application

I have an ASP.NET 3.5 Website (visual studio lingo), but the site continues to grow and is looking rather cowboyish among other things. I'd like to see this get converted into a Web Application (namespaces and all).
Is this something that can be easily done in Visual Studio? If not, are there any other tools out there that could create all of the namespaces, etc. automagically?
Well, it turns out that the option "Convert to web application" does NOT exist for "websites". The option "Convert to web application" does exist only for "web applications" !!!!
[emphasis mine]
So, here's the deal, to do the
conversion, you need to:
Add a new "Web Application" to your VS 2008 solution (File->Add->New
Project->C#->Web->ASP.NET Web
Application).
Afterwards, you copy all the files in the old "website" to your newly
created "web application", and
override any files created in it by
default
The next step is the most ugly, you need to "manually" add the references
in your "website" to the new "web
application". I thought the VS 2008
PowerCommands toy would do this for me
as it does copy references from other
project types, but it didn't. You have
to do it by yourself, manually, and
you have to be cautious in this step
if you have multiple versions of the
same assembly (like AJAXToolkit in my
case) or assemblies that have both GAC
and local versions or so.
Keep repeating the last step and trying to build the "web application".
You'll keep getting errors like "
'....' is unknown namespace. Are you
missing an assembly reference? ". Make
sure you have none of those except the
ones where '....' is replaced by the
IDs of the server controls you use. In
other words, keep adding references
and building the project until only
the errors that exist because of
missing .DESIGNER.CS or .DESIGNER.VB
files.
Afterwards, go to the "web application" root project node in VS
2008 solution explorer, and right
click it, then you WILL find the
option "Convert to web application".
What this option does is actually
making small changes to the "#Page"
and "#Control" directives of pages and
controls, and creating the required
.DESIGNER.CS or .DESIGNER.VB files.
Try building the "web application" again. If you get errors, see what
references may be missing and/or go
click the "Convert to web application"
again. Sometimes, if there's any error
other than those caused of missing
DESIGNER files, not all the
pages/controls will have those
DESIGNER files created for them.
Fixing the non DESIGNER problem and
clicking "Convert to web application"
again should do the job for this.
Once you are done successful VS build, you should be ready to go.
Start testing your web application.
Optionally, you can right click the
"web application" root project node in
VS 2008 Solution Explorer and click
"Properties" then go to the tab "Web"
to set the "web application" to a
virtual folder in IIS (you can create
new virtual directory from there in
VS). If you want to use the IIS
virtual directory that the old
"website" used, you need to remove
that from IIS first.
Update: When testing your pages, pay MOST ATTENTION to classes in
"App_Code" folder, especially those
with NO NAMESPACE. Those can be a big
trap. We had a problem with two
extension method overloads in the same
static class that had no namespace,one
extends DateTime? (Nullable)
and calls another overload that
extends DateTime itself. Calling the
other overload as extension method
passed VS 2008 compilation and gave us
a compilation error ONLY IN RUNTIME
(With IIS). Changing the call to the
other overload from calling it as
extension method to calling it as
normal static method (only changing
the call in the same class, calls from
other classes remained extension
method calls) did solve this one, but
clearly, it's not as safe as it used
to be in VS 2005. Especially with
classes with no namespaces.
Update2: During the conversion, VS 2008 renames your "App_Code" to
"Old_App_Code". This new name sounds
ugly, but DO NOT RENAME IT BACK. In
the "web application" model, all code
will be in one assembly. In runtime,
the web server does not know what web
project type you are using. It does
take all code in "App_Code" folder and
create a new assembly for it. This
way, if you have code in folder named
"App_Code", you'll end up with RUNTIME
compilation errors that the same types
exist in two assemblies, the one
created by VS, and the one created by
IIS / ASP.NET Development Server. To
avoid that. leave the "Old_App_Code"
with the same name, or rename it to
ANYTHING EXCEPT: "App_Code". Do not
place any code in such "App_Code"
folder and prefereably do NOT have a
folder with such name in your "web
application" at all.
I know this since before but forgot it
now as I have not used "website" model
for long :(.
Walkthrough: Converting a Web Site Project to a Web Application Project in Visual Studio at MSDN
If your website application grows.. it's better to split it into several projects. Conversion from Web Site project to Web Application project won't help much.
If you're having problems getting your new Web Application Project to build check the File Properties in Visual Studio of all 'helper' classes. For a project I was converting the Build Action was set to Content whereas it should have been Compile.
I've now successfully migrated one Website project to a web application and there is quiet a few gotchas to look out for.
Having ReSharper at your disposal helps a lot in refactoring the aspx files.
Set up your solution and create an empty WebApplication
Copy all file over
aspx files in website projects don't have a namspace. Wrap your classes in the appropriate namespaces
During copying, all my pages in subfolders got renamed to my project name and the foldername, so I got 40ish public partial class FolderName_Projectname : Page If neccessary rename all files using Resharper or manually.
If you encounter multiple errors like "There is already a member Page_Load() defined", this is most likely due to incorrect class names und duplication
After adding a namespace
Replace CodeFile in all aspx pages with Codebehind and especially pay attention to files i your subfolder. Make sure Inhertis="" doesn't contain the relative path. Your namespaces take care of everything. So the correct format is Inherits="Namespace.classname".
If your class has a namespace NaSpa and a filename foo.cs it would be Inherits="NaSpa.foo"
After you have prepared all your files (don't forget your master pages), run "Convert to web application". If you encounter errors afterwards, rinse and repeat.
If you encounter errors of the sort "TextBoxName can't be found are you missing a reference", make sure you did not forget to sanitize your aspx pages. A good indicator is to check the automatically generated designer files. If TextBoxName does not appear in there, the conversion did not succeed completely.
Resolve any missing dependencies.
Build
Create a New Web Application in VS 2010.
1. Using Windows Explorer copy all your files into you project folder.
2. In VS 2010 solution explorer show all files.
3. Select the files and folders - right click include in project.
4. Right click the project solution explorer and select Convert to Web Application.
There are quite a few small differences, such as the App_Code folder will get renamed to old_app_code - that surprisingly doesn't cause any errors. The TypeName on your object data sources and the inherits on the #Page tag might need the [ProjectName]. prefix appended globally. For example if your type name was "BusinessLogic.OrderManager" and your project name is InventorySystem you would need to change it to InventorySystem.BusinessLogic.OrderManager. Also a few display changes, such as required field validators don't default to red font anymore, they default to black.
I was facing the same problems initially. After following the Wrox Professional ASP.NET 4.0 book, I found the following solution for my case.
I first created a new web application. Copied all the website files into the web application folder. Right click on the application, and click conver to web application.
You might ask why you need to convert a web app into a web app. The answer is, that when you create a website, you simply code the .cs file where-ever required.
A web application, however declares .design.cs (or .vb) and a .cs file for the code and design section automatically.
NEXT: Remove all manual references, like 'Inherits' attribute in the PAGE directive, to other files in your website, since name spaces WILL take care of referencing the classes centrally.
I also faced a problem, since I had not included OBJ and BIN folder in my project.
If you think you are missing your BIN and OBJ folders, simply click the 'Show All Files' icon in the Solution Explorer and then right click on the missing folders and add to project. (to make sure they compile with the project.)
UPDATE:
As #deadlychambers points out in the comments: You can search everywhere by doing a "Ctrl + Shift + F" and then search for Inherits="(.*?)". This will find all occurrences and probably save you some time!
the default ASP name space does not seem to work anymore. So I cannot seem to call my User Controls.ascx pages from outside the page. Giving them a namespace and changing the default from ASP to my namespace seemed to work.

Resources