The name 'xxx' Does Not Exist in the Current Context

If you get this error when you are compiling then one thing that it might be is two aspx files pointing at one code behind file.

If you created your new page by doing a file > save as instead of using add new item then you are very likely to have unwittingly walked into this trap.

By default asp.net web pages come in pairs; an aspx file and a codebehind (either aspx.cs or aspx.vb depending on the language used).

When you did a file > save as only the .aspx file was duplicated and both point at the original aspx.cs / aspx.vb file!

The solution

To solve this problem you need to create a new file using the proper add item dialog (found in Project > Add New Item). You can copy over the aspx mark-up from your broken page if need to but make sure that you don't bring the broken @Page declaration with it or you will just point it back at the same code behind again!

If it has taken you a while to notice this error you may also have to extract any events that you have handled in the code behind.

No comments :