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
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!
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 :>
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"];
iman said
Very thank you,
i’ve been on the same situation without knowing how to solve the problem. You save my life dude.
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.
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
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.
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”);
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
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
Markus said
Thanks, that pointed me to the problem.
sa said
Thanks!!!!!!! saved me a lot of trouble
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.
Shaun said
I looked around the net ant it was cause i was referenceing the same code behind.
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. [:)]
Sridhar said
Thanks buddy…. It saved me lot of time!
Vinni said
Thanks so much. This helped.
Jacob said
YOU ARE THE SMARTEST PERSON IN THE WORLD
I LOVE YOU
shibbard said
lol
glad it helped
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)
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..
jon said
Bless you- I worked for a while on this one too
music said
What do you mean ?
Naren said
I was facing similar issues and stuck up
Thanks to you. I excluded the backup files and it worked.
Thomas said
Thanks for the info. I had two pages referencing the same codebehind file. Just excluding one solved the problem.
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
).
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.
Raju Visshwanath said
Thanks a lot. You saved me a day.
Daya said
Thank you so much… I was breaking my head for the last 2 days.. and ur suggestion worked for me…
Dave said
YES! thanks everyone — for me it was the ‘two pages referencing the same code-behind’
UGH!
asp classic was so much easier.
Aaron Clint said
WOOT! Lifesaver thanks for the post and thanks to google for finding it !
Matt said
You are a godsend. Thank you.
Anonymous said
Thanks to shibbard… This really helped!
Michael Daly said
That was my prob. Thanks for posting!
Terry Schuld said
Thank you thank you.
Kaye said
omg. chris! you saved my life!!!!!!!
thanks for the commenter named chris.
Tim said
Helped me too! Thanks for posting your experience!
Ravi said
Fantastic man, kudos to you! Solved a painful problem of ASP.NET and code-behind files.
guytuytu said
uiui
Raghav said
thanks its solve my problem please keep it up
dan said
thanks shibbard! your suggestion also worked for me! =)
Rupesh Bari said
Thanks a lot friend….its solve my problem please keep it up…
Amul said
Was really helpful…Keep up the good work..!!