Tuesday, 17 August 2010

label does not exist in current context


Error: label does not exist in current context


Scenario:
I have MyPage.aspx which have number of controls, it was working fine, till the point I made heavy changes. After making lot of changes to MyPage.aspx, when i tried to compile project, it started giving me error: label does not exist in current context. The problem here was my label control was there on .aspx page, but it was still giving me error at compile time.

Few things I tried which didn't worked

  • Try to switch design view and source view of MyPage.aspx
  • Tried to comment control and try to create same control again.
Cause of Issue:
I have notice even though label control was there on MyPage.aspx it was not their in MyPage.aspx.designer.cs. So there was something wrong with designer file.

Solution:
  • Delete designer file (Eg: MyPage.aspx.designer.cs)
  • Try to recreate designer file.
  • delete MyPage.aspx.designer.cs file, now right click on MyPage.aspx and select "Convert to web application" it will create new MyPage.aspx.designer.cs file.
  • At this point when I tried to create designer file i received error, unable to create designer file due to improper html. After i rectified html on MyPage.aspx and follow the same steps to create designer file, it have created MyPage.aspx.designer.cs
  • Finally my project gets compiled.

Moral of story
I figured out that VS.Net was giving weired error message. Even though label control was on MyPage.aspx it was throwing wrong error "label does not exist in current context". Appropriate error message would be something "Resolve improper html..." which would help me to figure out where exactly the problem is... "Don't trust on error message blindly"

No comments:

Post a Comment