473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Unable to set class variables in Page_Load

Hi,

I am having trouble setting class and session variables in the Page_Load
function in C#. No matter what I set the variables to in Page_Load they
are null when I try to access them in other code on the form.

- I have done this before in VB.Net and it has worked.
- I tried making the variables public (though they shouldn't need to be)
and got the same result
- If I move the same code into an on click event of a button on the web
form it works, so there is nothing in the code itself.

I am running VS 2003 on Windows 2000 Server. Below is some sample code.
If anyone can shed some light on this problem it would be much
appreciated. Also I tried setting breakpoints in Page_Load but the
debugger skips over it. Does anyone know how to get the debugger to step
through the Page_Load function?

private string strConn;
private MyObject obj;

private void Page_Load(object sender, System.EventArgs e)
{
strConn = ConfigurationSettings.AppSettings["ConnectionStr"];
obj = new MyObject(strConn);
Application["ConnectionString"] = strConn;
}

//obj will be undefined and strConn will be null
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 1819
David lbc wrote:
Also I tried setting breakpoints in Page_Load but the
debugger skips over it. Does anyone know how to get the debugger to step through the Page_Load function?


Actually this makes me think there is a larger problem here.
The sample code you posted seems ok to me.
The fact that the debugger doesn't get to the Page_Load method, on the
other end, is odd.

Is the page load in a codebehind of a page in the project that you
start in debugging mode?

If so, the debugger should get there.

Are you by chance using nUnit, or some other 3rd party library? I ask
because about a year ago I hit a similar problem when using a 3rd paty
tool... for some reason, as we added the references to it and tried to
use it, we lost the ability to debug through our code. I think we
simply had to close Visual Studio (all instances of it if you have
multiple ones open) and re-open it to make it work again....
Also,
since your Page_Load doesn't call any other method, I wonder if we are
slipping on some scoping issue.

Try

public class MyCodeBehind
{
private string str = "";

public void Page_Load(object sender, System.EventArgs e)
{
str = "Hello";
Test();
}
private void Test()
{
string s2 = str; //<-- check via debugger if str is undef here !
}

}

Finally, is this Page_Load method correctly wired to be the event
handler for the PageLoad event of the page that this codebehind class
is linked to?

HTH,
F.O.R.

Nov 16 '05 #2
>> Finally, is this Page_Load method correctly wired to be
the event handler for the PageLoad event of the page
that this codebehind class is linked to?


Ding. ding. ding.

The InitializeComponent function was missing the following line:

this.Load += new System.EventHandler(this.Page_Load);

I think I had moved an old page out of the way and renamed a new page,
so maybe this got lost somewhere in the process. Also, I am now able to
step through Page_Load with the debugger.

Thanks for the advice. Much, much appreciated.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stephen Adam | last post by:
Hi there, I've just started a c# project at work where I cannot use Visual Studio. I am trying to pass variables from one form to another. When I try to create an instance of the sending class...
4
by: Dean | last post by:
I finally got class session variables to work by putting the following in global.asax in the session_start: dim myDBComp as DBComp = new DBComp........ session("myDBComp") = myDBComp In each...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
3
by: Frank Esser | last post by:
Hi, I created a base class for my project from that all other pages inherit. Within this page in the OnLoad event I set some session object variables. My intention is that first of all the...
7
by: Göran Tänzer | last post by:
Hi, i've written a class which does some calculations for my web application. These informatinos are different for each page request - the current user is not important. i have about 10 aspx...
0
by: Henry | last post by:
I have written an ASP/VB.Net application via VS 2003 (Crystal V9) that uses MS Access 2000 as its database. I can export reports that have no linked sub reports for printing. However, I'm unable...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
0
by: bharathreddy | last post by:
Here I will given an example on how to access the session, application and querystring variables in an .cs class file. Using System.Web.HttpContext class. 1) For accesing session variables :...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.