Wednesday, June 02, 2010

label does not exist in current context

Error: label does not exist in current context

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.

  • 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"


Moderator said...

Thankq, It help a lot. Thanks once again for you details explanation.

Sans Pillars said...

Thanks a lot......
Wonderful solution

samso said...

Thank you it worked for me. Really appreciate the efforts and keep up the good job...

Hoài Nam said...

thanks you so much.

Dinh Dung said...

Change the class name in MyPage.aspx.designer.cs to current file name.

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:

Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape