HibboBlog

Web apps in ASP.Net

ASP.Net -> object does not exist in the current context – really??

Posted by shibbard on March 10, 2007

Another one of those problems that can stump you for a while. I often make backups of files as I progress and sometimes leave them in the website folder, this was fine with classic ASP but as I have found with ASP.Net can cause all sorts of problems.One issue I was having was trying to reference a component in my aspx page from the codebehind page and seeing the following error when trying to reference a label control:

“Label2 does not exist in the current context”

So I found that the problem was even though I renamed one of my old files it still contained the class that my codebehind was referencing (i.e. the class names were still the same) – and therefore was not seeing the newly added Label control to my new code.Shame the compiler doesn’t realise the duplication and give some indication.

Lesson: Remove any backed up files from the web folder and/or build directory to avoid pain :)

43 Responses to “ASP.Net -> object does not exist in the current context – really??”

  1. Dan Bailiff said

    THANK YOU!

    After reading through dozens of similar cries of help on the asp.net forums, I could not find a solution to this problem. But you described my situation exactly, so I excluded my backup copies of the file in question and it immediately compiled.

    THANK YOU!

  2. shibbard said

    Hi Dan, I am glad this helped someone as it had me baffled for absolutely ages.

    I never did see this suggestion anywhere and thought if I could just get it up on the net somewhere it may just help someone else – which it seems it has and so it seems was worth while. Thanks for letting me know :>

  3. Yasser Ahmed said

    This error normally occurs when you are trying to access the value of a session variable such as Session["UserID"] and you’re not doing that from a webform, usercontrol or a class that inherits from System.Web.UI.

    In this situation you can still access the session variable, but using a different path.
    For example to access a session variable named UserID you would normally use Session["UserID"]; however, if the error The name ‘Session’ does not exist in the current context is returned, use the following path for retrieving the value of the session variable:

    HttpContext.Current.Session["UserID"];

  4. iman said

    Very thank you,

    i’ve been on the same situation without knowing how to solve the problem. You save my life dude.

  5. Brian said

    Helped me too! Here’s a twist on it. Another developer had copied an aspx page from another one and never changed the codebehind reference! So, adding controls to the original aspx and compiling resulted in an error in the .cs codebehind. However, it was not apparent the problem was that another aspx page was using the same codebehind.

  6. shibbard said

    funny it seems to be more common than i thought – but certainly wasn’t easy for me to track down when i came accross it as i couldn’t find any reference to the problem elsewhere

    nice to see this has helped a few people out :)

  7. Pavithra said

    Even I faced the same problem.I added this line of code

    Label = (Label).FindControl(“”);

    This worked fine, as the label control is the child control of the Form.So label control is not accessible directly.

  8. Pavithra said

    I dont know why the code is not visible properly in my previous comment.Anyway,this is the line.

    Label lblname =
    (Label)formname.FindControl
    (“lblname”);

  9. Jay said

    Thanks a million !!!!1

    I was working on these problem since last two long days…but your suggestion works…

    thank you very much.

    Regards,

    Jay Khanpara

  10. metaldude said

    I’ll give you $5 for saving my time!!!! (I know it’s worth more but that’s all I got at the moment :-) )

    Thanks

  11. Markus said

    Thanks, that pointed me to the problem.

  12. sa said

    Thanks!!!!!!! saved me a lot of trouble

  13. moe! said

    ACTUALLY.. this ALMOST solved my problem.

    my actual problem was that I had more than 1 page inheriting from/pointing to the same code file.

    So in page2.aspx at the top, i had
    CodeFile=”page1.aspx.cs” Inherits=”page1″

    while really the top of page2.aspx should have had:
    CodeFile=”page2.aspx.cs” Inherits=”page2″

    this happened because i copy-pasted the code from page1.aspx into page2.aspx.

  14. Shaun said

    I looked around the net ant it was cause i was referenceing the same code behind.

  15. Prashant Vedpathak said

    Thank you,

    Simon Hibbard, I was tackling with this problem. I got some clue about this problem previously but u gave me the whole answer. Thank u very much. [:)]

  16. Sridhar said

    Thanks buddy…. It saved me lot of time!

  17. Vinni said

    Thanks so much. This helped.

  18. Jacob said

    YOU ARE THE SMARTEST PERSON IN THE WORLD

    I LOVE YOU

  19. shibbard said

    lol

    glad it helped :)

  20. linsen said

    I read one solution in the web “http://forums.asp.net/p/1006588/1736105.aspx”. This says to remove the build option, which i didn’t find satisfactory. But this was the correct solution(to remove any backup files, if any) :-)

  21. Vishnu said

    The above solution not solving my problem.. let me know wat goes worng here.. Plz its a quite urgent…

    MySqlConnection sConn = new MySqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["mysql"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
    sConn.Open();
    MySqlDataAdapter da = new MySqlDataAdapter(“select appl_projInfoInvoiceNumber, appl_DateOfApplication, appl_DateInvoiced, appl_clientDetailsCompany, appl_projInfoProducedFor, appl_UserId, appl_type, appl_LicenseDetailsAmountText from sync_license_apps where appl_status = ‘invoiced’”, sConn);
    DataSet ds = new DataSet();
    da.Fill(ds, “sync_license_apps”);
    Gridview1.DataSource = da;
    GridView1.DataBind();
    sConn.Close();
    }

    thankss..

  22. jon said

    Bless you- I worked for a while on this one too

  23. music said

    What do you mean ?

  24. Naren said

    I was facing similar issues and stuck up

    Thanks to you. I excluded the backup files and it worked.

  25. Thomas said

    Thanks for the info. I had two pages referencing the same codebehind file. Just excluding one solved the problem.

  26. Chris said

    Another possible solution, I downloaded source code from another developer which had been created as a web application however it did not include the designer.cs file. I then added the file to a website project. Eventual solution was to convert the project to a web application (right click on your project in solution explorer for those who dont know where to find it :) ).

  27. Soheil said

    Hi , thank u very much . my problem is i have two class with the same name ..sorry for compiler that does not detect there two class with tha same name in two different file.

  28. Thanks a lot. You saved me a day.

  29. Daya said

    Thank you so much… I was breaking my head for the last 2 days.. and ur suggestion worked for me…

  30. Dave said

    YES! thanks everyone — for me it was the ‘two pages referencing the same code-behind’

    UGH!

    asp classic was so much easier.

  31. Aaron Clint said

    WOOT! Lifesaver thanks for the post and thanks to google for finding it !

  32. Matt said

    You are a godsend. Thank you.

  33. Anonymous said

    Thanks to shibbard… This really helped!

  34. Michael Daly said

    That was my prob. Thanks for posting!

  35. Terry Schuld said

    Thank you thank you.

  36. Kaye said

    omg. chris! you saved my life!!!!!!!

    thanks for the commenter named chris.

  37. Tim said

    Helped me too! Thanks for posting your experience!

  38. Ravi said

    Fantastic man, kudos to you! Solved a painful problem of ASP.NET and code-behind files.

  39. guytuytu said

    uiui

  40. Raghav said

    thanks its solve my problem please keep it up

  41. dan said

    thanks shibbard! your suggestion also worked for me! =)

  42. Rupesh Bari said

    Thanks a lot friend….its solve my problem please keep it up…

  43. Amul said

    Was really helpful…Keep up the good work..!!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>